JS проверка на NaN: как проверить, является ли число NaN в JavaScript
![]()
В JS проверка на NaN затруднена там, что это тип данных, который равняется «ничем». NaN — это «Not a Number» и переводится как «не число». Обычно такое значение возвращается, когда невозможно завершить работу математической функции или когда в значении математической функции расположено «не число». На практике, NaN появляется тогда, когда в JS-скрипте появляется ошибка.
Может возникнуть простой вопрос: для чего в JavaScript NaN, если это не число, не строка и даже не «пустота»? NaN редко когда применяется специально. А его история возникновения тянется еще с первых лет создания JavaScript. Те, кто работает с JavaScript, знают, что это не строгий и многое прощающий язык программирования. NaN в него был введен для того, чтобы в случае возникающих ошибок в скрипте, программа не оканчивала собственное выполнение, а продолжала работать вернув в качестве значения ошибки NaN.
Проверка на NaN в JS
-
функция «isNaN()» при проверке вернет «true» в двух случаях: если при проверк е значение уже NaN и если оно станет NaN после попытки преобразования его в число;
-
метод «Number.isNaN()» вернет «true» только если значение при проверк е уже является NaN.
Проверка на NaN в JavaScript: безотказный метод
Проверка на NaN в JS возможна, но для этого нужно выстраивать специфичные конструкции из кода. Например, можно попробовать такую конструкцию:
function sanitise(a) <
if (isNaN(a)) <
return NaN;
>
return a;
>
console.log(sanitise(‘тут введите какое-то значение’));
Вы можете попробовать этот скрипт в действии и в функции «console.log()» ввести разные значения: строки, числа, NaN, underfilled и др. Таким образом вы сможете убедиться, что такая проверка на NaN в JS работает безотказно.
Заключение
Сегодня мы познакомились с таким типом данных в JavaScript, как NaN. Самое важное, что теперь вы знаете как проводится проверка на NaN в JS. Она нужна не часто, так как NaN — это специфическое значение, которое получается самостоятельно, но может быть полезной.
Мы будем очень благодарны
если под понравившемся материалом Вы нажмёте одну из кнопок социальных сетей и поделитесь с друзьями.
JavaScript NaN (Not a Number) and the isNaN methods.
In JavaScript NaN is an weird number value in javaScript that means Not A Number, but yet the type of the value is Number. So yes if the type of operator is used with a NaN value that type is indeed number. However that is not the last thing that is a little weird about the NaN value in javaScript.
This value has some unique things going on with it aside from the fact that the data type is a Number, yet it is called Not A Number. For one thing it does not equal anything, including itself, which means that special methods must be used to test if a value is NaN or not. On top of that there are problems with the native and user space methods that are used to test for NaN that a javaScript developer needs to look out for. Many of them will return what many will observe as a false positive for certain values other than NaN, such as the undefined value, which in turn further complicating the process of testing for NaN.
The value will come up now and then often as a result of an expression, when something in that expression ends up being undefined or a sting value that can not parse to a number, and thus ends up being the NaN value. So the existence of the NaN value is closely tied with the typeless nature of javaScript when it comes to working out expressions involving numbers and strings. When values such as these are combined with math operators such as Multiplication then the resulting value will be NaN. So there is a need to know how to account for the possibility of JavaScript NaN being a possible value, and how to deal with it when working out some code.
1 — JavaScript NaN basics
In this section I will just be going over some basics with the NaN number value in native core javaScript. So then this section will just focus on how it is possible to end up with a NaN value to begin with, and also certain aspects of the value itself that is a bit odd. I will be keeping this examples fairly basic in this section of course, but I still assume that you have at least some background when it comes to getting started with javaScript. I also often use this section to bring up various things that you might want to know also before continuing to read the rest of the post.
1.1 — The source code examples here are on github
The source code examples here can be found on my Github test vjs repository. All the source code for my various other posts on javaScript in general can also be found there. If you find something wrong with one of the source code examples here, or if you think there is something of value to add that is one way to go about making a pull request. There is also the comments section at the bottom of this post that might also be a good way to go about bring something up.
1.1 — Some ways to end up with a JavaScript NaN value
First off there are a number of ways to end up with the value of NaN to begin with. For example dividing the value of zero by zero will result in NaN. Another way to end up with NaN would be to multiply a string that can not effectively be converted to a number value. There is also just the plain old javaScript NaN literal that will directly result in the value of NaN.
So now that we have some basic ways of knowing how to end up with NaN figured out lets look into the ways to go about knowing if we have a NaN value, and so forth.
1.3 — Not a Number is actually a Number
The NaN value is an acronym for Not-a-Number, but yet using the typeof operator with a NaN value will result in the type of number . The reason for this has to do mainly with javaScripts typeless nature when it comes to what values a variable can contain. In javaScript a variable can be a number value, but at any moment that same variable can end up storing a string value for example.
So when it comes to evaluating expressions, it is possible for a variable that should hold a number will in fact end up storing a string value. This is not always a problem if the string value can effectively be converted to a valid number value, unless for some reason it can not be, in which case the NaN value is a result. So this Not a Number value typically means that somewhere a string value failed to property convert to a Number value, and thus the value is Not a Number, but the type of NaN is still a Number. It is a bit confusing but the term Not a Number means the value from which the Number was converted from, rather than the type of the NaN value.
So because the NaN is a number methods that are part of the Number prototype can still be used with a NaN value such as the Number.isFixed method. In addition to this the fact that NaN is a number can be used to ones advantage when it comes to making a user space is NaN number. The type of the value can be checked first, and if the type if not number return false right away. After we have found that the value is not a string we can not convert the value to a String, and test if the result of that is equal to the string NaN if so return true. If all else fails assume that the type is number and that the value of the number is not NaN so return false.
1.2 — The NaN value does not equal itself
What is strange about NaN is that it does not equal anything, not even itself. Because of this it makes testing for NaN a little move involved then just using the equality or identity operators to do so. There is a well supported native method called isNaN, but also Number.isNaN both of which work differently, more on that later.
So now we have the basics of NaN out of the way. The NaN value is something that can end up happening as a result of an expression where proper filtering and type checking is not preformed. In addition if we have a NaN value ot is not always so easy to test for it, so special methods are needed to find out if a value is NaN or not.
2 — The native javaScript isNaN method
So to some extent the native javaScript isNaN method works as expected, but it also returns true for values that are not NaN, such as undefined.
This behavior may not be wrong necessarily because in a way values like undefined, and null are Not Numbers also of course. Still it may not really be the behavior that is expected when using a method called isNaN as often the expectation might be that the method should only return true when a value is NaN and only NaN. So with that said lets look at some more options when it comes to finding out if a Value is NaN.
3 — The Number.isNaN method
So the Number.isNaN method works as expected if what is expected is for the method to return true only if the given value is NaN and only NaN.
The only problem with Number.isNaN is that it does not work on any version of IE, there is no support at all. So then this might be a reason why you might want to use some kind of user space option for testing for NaN if you care about supporting those older platforms for some reason. With that said lets look at making a user space option as that is nit so hard to do when it comes to working out a quick user space solution.
4 — Writing an isNaN method
When going vanilla js style with a project, a method that tests for NaN might need to be part of a custom micro frame work, or utilities library of sorts. Making a method to do this is not so hard, one way is to just return false if any type other than a Number is given, then convert to a String and test against the String ‘NaN’. In the off chance that the string ‘NaN’ is given then it will return false because the type is is a String, and thus will not get to the part where we are converting the string value to a string. So in this section I will be going over some user space examples of testing for NaN, that mainly take this kind of approach.
4.1 — The example from the basic section
In the basic section I covered this user space example right here.
This might work okay when it comes to having a vanilla javaScript is NaN method, but it can be crunched down a little.
4.2 — Crunching it down a little
One step forward might be doing something to not need to additional second if statement.
This seems to behave like that of Number.isNaN, but will work on old browsers. It seems to work okay, but maybe you might prefer to use the method that exists in lodash that makes use of the fact that NaN is the only primitive value that does not equal itself. Also there is the idea of just using whatever it is that they are using in lodash as a single stand alone method, or in one way or another find out a simple single expression type solution for this.
4.3 — slick one line example
Taking a moment to look at the source code of the popular javaScript framework know as lodash, I found a isNan method that looks like this:
So of course this is a Nice slick one liner. On top of that this kind of method makes use of the Nature of NaN not equaling itself. However the check for the number type still needs to be preformed because the string value of ‘NaN’ will still return true.
4.4 — Monkey patching Number.isNaN
Monkey Patching is generally frowned upon, but generally only if you are extending built in Objects with non standard methods. In this case monkey patching Number.isNaN support is just making sure that something that should be there is there.
6 — Conclusion
So the javaScript NaN value is often the result of faulty operations such as multiplying a number by a string that can not effectively be converted to a number value. The value can often end up being the result of certain function calls and so forth especially when taking values from user input, so parsing of value should be handled well, or testing for NaN should be preformed.
Sooner or later I will be getting around to editing and expanding this post a bit more, however I think there is only so much more to cover. I think that in the future the only thing left to do would be to just write a bit more about various code examples that typicality result in NaN values, and additional ways to not just test for NaN but also to prevent it from happening to begin with.
isNaN
Функция isNaN() определяет является ли литерал или переменная нечисловым значением ( NaN ) или нет. При работе с функцией необходимо проявлять осторожность так как она имеет свои особенности. В качестве альтернативы можно использовать метод Number.isNaN() из ECMAScript 6, или дополнительно проверять литерал или переменную на нечисловое значение при помощи typeof .
Синтаксис
Параметры
Литерал или переменная которые будут проверяться на нечисловое значение.
Описание
Для чего нужна функция isNaN ?
В отличие от других возможных значениях в JavaScript, при работе с значением данного типа невозможно полагаться на == и === для определения, является ли переменная или литерал нечисловым значением ( NaN ) или нет, так как проверки NaN == NaN и NaN === NaN в качестве значения вернут false . Следовательно, для проверки нужна функция isNaN .
Примечание:
Для альтернативной проверки переменной на NaN без использования функции isNaN() можно воспользоваться конструкцией x !== x
Генерация значения NaN
Значение NaN генерируются арифметическими операциями, результатом которых является undefined или unrepresentable. Такие условия не обязательно обозначают переполнение стека. NaN также может являться результатом попытки преобразования числа в строку, или значения, не имеющего эквивалента в простых числовых примитивах.
Например, деление нуля на нуль возвращает NaN — но деление других чисел на 0 не возвращает NaN.
Особенности поведения
С самых ранних версий функции isNaN её поведение для не числовых переменных или литералов было довольно-таки запутанным. Когда аргументом функции isNaN является переменная, тип которой не Number, она преобразуется к типу Number . Полученное значение затем проверяется, является ли оно NaN . Таким образом для не числовых значений, которые можно преобразовать в числовой тип без не-NaN значения (в частности, пустая строка или логические примитивы, которые преобразуются в 0 или 1), возвращаемое значение «false» может быть полной неожиданностью; пустая строка преобразуется в «not a number.» Путаница связана с тем, что «not a number» имеет определённое значение, описанное в стандарте IEEE-794 чисел с плавающей точкой. Функцию стоит воспринимать в качестве ответа на вопрос, «Является ли это значение корректным числом по стандарту IEEE-794?»
How do you check that a number is NaN in JavaScript?
I’ve only been trying it in Firefox’s JavaScript console, but neither of the following statements return true:
![]()
![]()
33 Answers 33
For checking whether any value is NaN, instead of just numbers, see here: How do you test for NaN in Javascript?
I just came across this technique in the book Effective JavaScript that is pretty simple:
Since NaN is the only JavaScript value that is treated as unequal to itself, you can always test if a value is NaN by checking it for equality to itself:
Didn’t realize this until @allsyed commented, but this is in the ECMA spec: https://tc39.github.io/ecma262/#sec-isnan-number
![]()
As far as a value of type Number is to be tested whether it is a NaN or not, the global function isNaN will do the work
For a generic approach which works for all the types in JS, we can use any of the following:
For ECMAScript-5 Users:
For people using ECMAScript-6:
And For consistency purpose across ECMAScript 5 & 6 both, we can also use this polyfill for Number.isNan
please check This Answer for more details.
![]()
You should use the global isNaN(value) function call, because:
- It is supported cross-browser
- See isNaN for documentation
I hope this will help you.
As of ES6, Object.is(..) is a new utility that can be used to test two values for absolute equality:
![]()
NaN is a special value that can’t be tested like that. An interesting thing I just wanted to share is this
This returns true only for NaN values and Is a safe way of testing. Should definitely be wrapped in a function or atleast commented, because It doesnt make much sense obviously to test if the same variable is not equal to each other, hehe.
![]()
NaN in JavaScript stands for «Not A Number», although its type is actually number.
To check if a variable is of value NaN, we cannot simply use function isNaN(), because isNaN() has the following issue, see below:
What really happens here is that myVar is implicitly coerced to a number:
It actually makes sense, because «A» is actually not a number. But what we really want to check is if myVar is exactly of value NaN.
So isNaN() cannot help. Then what should we do instead?
In the light that NaN is the only JavaScript value that is treated unequal to itself, so we can check for its equality to itself using !==
So to conclude, if it is true that a variable !== itself, then this variable is exactly of value NaN: