use-isnan
检查 NaN
时需要调用 isNaN()
配置文件 中的
"extends": "eslint:recommended"
属性启用了该规则
在 JavaScript 中,NaN
是 Number
类型的一个特殊值。它用于表示由 IEEE 二进制浮点算术标准指定的双精度 64 位格式表示的任何 "not-a-number" 值。
因为 NaN
在 JavaScript 中是独一无二的,它不等于任何东西,包括它自己,所以与 NaN
的比较结果令人困惑:
NaN === NaN
或NaN == NaN
评估为假NaN !== NaN
或NaN != NaN
评估为真
2TeJaiwwKQk3Ctky/9/+lrDRpo8+iMSO9H2q5onKPr5YfmipuaTs6/l2TE6Ng2taJJBo2hH3NUn9dADxmpd9gQsVIuKvBe7ehjFgcfCtifLgc6xrp8wcSppKFnUt4YrpcnoQa1sUQaSLNkpTpPb18VsR+KXNHsTN1mvkEGlZwaJWQ8Wckcm+D+Oo+IUfMcxK
规则详情
4DxZeEsifoOUJsA06MGtWmAG4ftVj9ThYJkzANDciPjHLks1syQV6q80J+8w4zGY
7jd2bhXiUGDmBKu+cZeo8q9FHH9BbCCMY2Txw3OX9o2XrFfZEB7Ku3Dcx85/CBwT
/*eslint use-isnan: "error"*/
if (foo == NaN) {
// ...
}
if (foo != NaN) {
// ...
}
if (foo == Number.NaN) {
// ...
}
if (foo != Number.NaN) {
// ...
}
NwldEOIuHKyv0R+3pkUiuSlq7yzV1WsYiQ97qEISbUzf2ki35mZ0HV/HKFTSzvSb
/*eslint use-isnan: "error"*/
if (isNaN(foo)) {
// ...
}
if (!isNaN(foo)) {
// ...
}
选项
zaHvPcgsugX4MUASJPYCXtmNqHTCIN7Divdsz+quHFoaa/BuNmgc1dQt0ZnFErfr+kiakkqsltoyosX08IzMxQ==
- 0ZQ5dKvy/mevmno+x2NfqE4kU7LPMNO+HZfhsgg5638VdQ5bpAGHnUIKF6EclbZjqQ/xXQHUleBDtEyKkxmEBcX/ZWP3tuXDcb+DUhSbZiFq4LkS5VWPKW8YHo0RYBnIhNjRoxhyawHV6t9gWXIlfQQmsLw0H4BilAnNwuZ8DxzlsrnQYLZAb9gmBb0IVn3+77iPYrBk9ZuQzLvQp6cWHtOUCfiY7kbGQ+2MF3JPZ6nwzx+E2RUTBvimRvYdSYYGNUr0vC0CTW7gYsXHiybfYhbdzd4esIzuOIY9VmO55rQXqeqelZu19qnZmWPED67A1cfgRUvkvDGsX5CAffzuUvV3OVVj7czuJ10yEfT7CjRAMtsIhM7/t67XwfIcwRfOMchSXK5Ype8+dNMrdxiTS6S/xE3xBoWCyRsUGrMllpqOkcsbO34u1emsfHyMU6TjNXahgF3dNXyCvRO1XAxeyF6M0BfVyW+MA94uoYtWgyMMQnffdiNBDi5WHsX5rcxCTQqUG9hKc2a5R68q2Iko9xIXMw8qs11qsSRKw6/YVFx8JvUB5E/aU07NmzRzmFNjMXwsNN6KJoaziQFAI6Butc5ikiSSRDuTFGQ8kCiABP93tTbvECTDOh9ssgbpPIYv
enforceForSwitchCase
18tRXL8agovYY5TNwpLhPdN/LPIenfZoo+Sth/zhajmxwv/LwmmOKzMsuTJIp+foSK9w7YCfsMGrYiRlV7QmAtJD13QvI2voOcwNdBZ/QHGaqj90X+li2yMiJiJd2ZHP80y7yIts6/Lv6nbdttJyLKsPaSdFif8SeTF+PIrOZSsyNl7ZDCS6Sx4yff3qQ1Ny4rMXkLo7J8qs6DGWHNMCNC/Q7RAwYiu4af2I5lUm56EAEFjfi2QoBU07afGKsuCano26l+j4CaekSng5X3IHgq/fMfgCKCcQziMletB5/dBW9iet4apoRtNfKJKsTFHA
7jd2bhXiUGDmBKu+cZeo8q9FHH9BbCCMY2Txw3OX9o1ZX/KazlikvP9gvWkEe5pc+O0y9/qw+LQ83+Snyc/c6RXBBcLvHtZ0SQ69iFQLk1KnEQ4WU62wWsZ86o/azO6kFTNI7H4GmTRzCNA9iMpB9i/T7oSX6HJ4gDVXMGtT/yA=
/*eslint use-isnan: ["error", {"enforceForSwitchCase": true}]*/
switch (foo) {
case NaN:
bar();
break;
case 1:
baz();
break;
// ...
}
switch (NaN) {
case a:
bar();
break;
case b:
baz();
break;
// ...
}
switch (foo) {
case Number.NaN:
bar();
break;
case 1:
baz();
break;
// ...
}
switch (Number.NaN) {
case a:
bar();
break;
case b:
baz();
break;
// ...
}
NwldEOIuHKyv0R+3pkUiuSlq7yzV1WsYiQ97qEISbUwQIOR6/8tqMvp3uxIfwlWkFq2XZEZIYSz1vqpdRL6TLHNbKHuyIOG3Cyp3WE5U8VsJrPoD2qDhpW2+h1dWpIXwigQ0iWRZMlyU2fAMh5gkDUREJ3zo5zDChk0g/93CFWk=
/*eslint use-isnan: ["error", {"enforceForSwitchCase": true}]*/
if (Number.isNaN(foo)) {
bar();
} else {
switch (foo) {
case 1:
baz();
break;
// ...
}
}
if (Number.isNaN(a)) {
bar();
} else if (Number.isNaN(b)) {
baz();
} // ...
NwldEOIuHKyv0R+3pkUiuSlq7yzV1WsYiQ97qEISbUwQIOR6/8tqMvp3uxIfwlWkFq2XZEZIYSz1vqpdRL6TLHNbKHuyIOG3Cyp3WE5U8VsJrPoD2qDhpW2+h1dWpIXwGEBqRlgLVqxupixLcWWRg1LtPj40ThBtyFDwJQj0R5w=
/*eslint use-isnan: ["error", {"enforceForSwitchCase": false}]*/
switch (foo) {
case NaN:
bar();
break;
case 1:
baz();
break;
// ...
}
switch (NaN) {
case a:
bar();
break;
case b:
baz();
break;
// ...
}
switch (foo) {
case Number.NaN:
bar();
break;
case 1:
baz();
break;
// ...
}
switch (Number.NaN) {
case a:
bar();
break;
case b:
baz();
break;
// ...
}
enforceForIndexOf
lPMO11BG4h/Yfav+LqGeTugjSWhA5HKj2Xhs8JTlWjs1Yw9YsSBm0tOEXtgCJ1/CVxN0X6gvW6Bp/c3IurcejmWA6LVi1nOMzIvG1igS+BoFLlcnu0qq01lDZ8blOWxX
- pDCNLi12EOTkNNIh0FP966VDU2xAZZUSseX9DGxY5R73gOdQN9CA28a4ExEysScdALGJLqn/h4dXOEA6vsP4dav1pMy/fLety7z5MF+3E6qV9dlORlT3mRUoO1qdxi/Wz2IePWH0SCEXbv2pCxBqrWpjUD7V1yJl8zRlaRT6dZFdB/CmePgrdxvYy13B9MOcOrPecDPU1O64Sk4QipevtcSQQurGw4Vs9QYZvYxU8OMf6W2V+2nQLrSR0S0oxvA0n7S6RnzN3PDVUsj8yy40+Q==
pBBNlWTHc8qaP6ADclxQUoU67IZig3tVmFogmIUPS4c23jiaR5ZYFrKD9bLA0Ws+0i6MBNoj9jVhhaFhgpgXKnpBKl3LWinSQIQiZ51w9GHXORwAknR+whX8h43C38rYTS8hEdeteTsrYGv/v+2Y+WyDDQa2mdCNNJPXxgz4uWvkacSp0ks+aMM1df5r0TyLzMHMxiYLQWldDmEg7ngcJg==
VXR1JMPCsk7bWaPB7LPl0E23deeIw/3kuE6Q1nEbbLBHG3gxJDjOq5bN0U920tGZjGutNx36RrsSvg0ta4WI9+MUH3nYnmPaptljkyNF3PNXcEeDo6/QFFUb25cEDAHcyeUjj7ez+WryMSj466I92hSg2l0wl2eDt0VjYzpDS7FEaGIFIJFCDQkB2DmwuvWMaWd7JH0WJXpfO+N+V21XT/RUVaBZL5uSodp6J1wgiN3rg1qpbL87jc7maCgmWIdp
7jd2bhXiUGDmBKu+cZeo8q9FHH9BbCCMY2Txw3OX9o1ZX/KazlikvP9gvWkEe5pcHUIhCOxDTMFIRYut6LJoJSoY9hkfLYjrhWwjLzQv7+XqULFmgfUUFYmZKQR4MOJAnZhYH8EkRdcRGfvIB09n6Q==
/*eslint use-isnan: ["error", {"enforceForIndexOf": true}]*/
var hasNaN = myArray.indexOf(NaN) >= 0;
var firstIndex = myArray.indexOf(NaN);
var lastIndex = myArray.lastIndexOf(NaN);
NwldEOIuHKyv0R+3pkUiuSlq7yzV1WsYiQ97qEISbUwQIOR6/8tqMvp3uxIfwlWk2j8Qp8KmR8TQ41QAaBBI37hHUzGuErxNGUkQEtG6bRRenqp5OCfyygUQcG90VIfLgfLp9i0GLfdtLEcdY8n8rA==
/*eslint use-isnan: ["error", {"enforceForIndexOf": true}]*/
function myIsNaN(val) {
return typeof val === "number" && isNaN(val);
}
function indexOfNaN(arr) {
for (var i = 0; i < arr.length; i++) {
if (myIsNaN(arr[i])) {
return i;
}
}
return -1;
}
function lastIndexOfNaN(arr) {
for (var i = arr.length - 1; i >= 0; i--) {
if (myIsNaN(arr[i])) {
return i;
}
}
return -1;
}
var hasNaN = myArray.some(myIsNaN);
var hasNaN = indexOfNaN(myArray) >= 0;
var firstIndex = indexOfNaN(myArray);
var lastIndex = lastIndexOfNaN(myArray);
// ES2015
var hasNaN = myArray.some(Number.isNaN);
// ES2015
var firstIndex = myArray.findIndex(Number.isNaN);
// ES2016
var hasNaN = myArray.includes(NaN);
已知限制
aGr+h3aBLGijQGGyQjOqLyITIAgXu+VwNlDIJdLv+73wDPQOpcOBeQahtz98FrzlouVGwotlFw98lWg2PWGApwD2s3wLlVXqDsjFpm4aWAz6kxJKS9snArZhgh094TVa