Is null evaluated to 0 and undefined to NaN on arithmetic expressions? Is it safe or correct to assume this?
Yes, it is. An "arithmetic expression" would use the ToNumber operation:
Argument Type | Result
--------------+--------
Undefined | NaN
Null | +0
β¦ |
It is used in the following "arithmetic" expressions:
- prefix/postfix increment and decrement
- the unary
+and-operators - the
+operator if none of the two arguments is a string - subtraction, multiplication, division and modulo operation
- relational operators if not both arguments are strings
It is not used by the equality operators, so null == 0 is false (and null !== 0 anyway)!
Is null evaluated to 0 and undefined to NaN on arithmetic expressions? Is it safe or correct to assume this?
Yes, it is. An "arithmetic expression" would use the ToNumber operation:
Argument Type | Result
--------------+--------
Undefined | NaN
Null | +0
β¦ |
It is used in the following "arithmetic" expressions:
- prefix/postfix increment and decrement
- the unary
+and-operators - the
+operator if none of the two arguments is a string - subtraction, multiplication, division and modulo operation
- relational operators if not both arguments are strings
It is not used by the equality operators, so null == 0 is false (and null !== 0 anyway)!
It seems safe to assume so since, in an arithmetic expression (e.g. addition), the method ToNumber would be called on it, evaluating NaN and +0 from undefined and null respectively:
To Number Conversions
βββββββββββββββββ¦βββββββββββββββββββββββββββββββββββββββββββββ
β Argument Type β Result β
β ββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββ£
β Undefined β NaN β
β β β
β Null β +0 β
β β β
β Boolean β The result is 1 if the argument is true. β
β β The result is +0 if the argument is false. β
β β β
β Number β The result equals the input argument (no β
β β conversion). β
β β β
β String β See grammar and note below. β
β β β
β Object β Apply the following steps: β
β β 1. Let primValue be ToPrimitive(input β
β β argument, hint Number). β
β β 2. Return ToNumber(primValue). β
βββββββββββββββββ©βββββββββββββββββββββββββββββββββββββββββββββ
ECMAScript Language Specification - ECMA-262 Edition 5.1
Why is `typeof null === 'object'` in JavaScript? The 30-year story of a bug we can't fix
javascript - Why is isNaN(null) == false in JS? - Stack Overflow
How do I check for null values in JavaScript? - Stack Overflow
ES6 is great, until itβs not
What a terrible first example. This is shorter and easier to read;
let { inputClass } = manager.options;
This is nothing to do with ES6 and just about writing clear code.
More on reddit.comVideos
I believe the code is trying to ask, "is x numeric?" with the specific case here of x = null. The function isNaN() can be used to answer this question, but semantically it's referring specifically to the value NaN. From Wikipedia for NaN:
NaN (Not a Number) is a value of the numeric data type representing an undefined or unrepresentable value, especially in floating-point calculations.
In most cases we think the answer to "is null numeric?" should be no. However, isNaN(null) == false is semantically correct, because null is not NaN.
Here's the algorithmic explanation:
The function isNaN(x) attempts to convert the passed parameter to a number1 (equivalent to Number(x)) and then tests if the value is NaN. If the parameter can't be converted to a number, Number(x) will return NaN2. Therefore, if the conversion of parameter x to a number results in NaN, it returns true; otherwise, it returns false.
So in the specific case x = null, null is converted to the number 0, (try evaluating Number(null) and see that it returns 0,) and isNaN(0) returns false. A string that is only digits can be converted to a number and isNaN also returns false. A string (e.g. 'abcd') that cannot be converted to a number will cause isNaN('abcd') to return true, specifically because Number('abcd') returns NaN.
In addition to these apparent edge cases are the standard numerical reasons for returning NaN like 0/0.
As for the seemingly inconsistent tests for equality shown in the question, the behavior of NaN is specified such that any comparison x == NaN is false, regardless of the other operand, including NaN itself1.
I just ran into this issue myself.
For me, the best way to use isNaN is like so
isNaN(parseInt(myInt))
taking phyzome's example from above,
Copyvar x = [undefined, NaN, 'blah', 0/0, null, 0, '0', 1, 1/0, -1/0, Number(5)]
x.map( function(n){ return isNaN(parseInt(n))})
[true, true, true, true, true, false, false, false, true, true, false]
( I aligned the result according to the input, hope it makes it easier to read. )
This seems better to me.
JavaScript is very flexible with regards to checking for "null" values. I'm guessing you're actually looking for empty strings, in which case this simpler code will work:
if(!pass || !cpass || !email || !cemail || !user){
Which will check for empty strings (""), null, undefined, false and the numbers 0 and NaN.
Please note that if you are specifically checking for numbers, it is a common mistake to miss 0 with this method, and num !== 0 is preferred (or num !== -1 or ~num (hacky code that also checks against -1)) for functions that return -1, e.g. indexOf).
To check for null SPECIFICALLY you would use this:
if (variable === null)
This test will ONLY pass for null and will not pass for "", undefined, false, 0, or NaN.
Additionally, I've provided absolute checks for each "false-like" value (one that would return true for !variable).
Note, for some of the absolute checks, you will need to implement use of the absolutely equals: === and typeof.
I've created a JSFiddle here to show all of the individual tests working
Here is the output of each check:
Null Test:
if (variable === null)
- variable = ""; (false) typeof variable = string
- variable = null; (true) typeof variable = object
- variable = undefined; (false) typeof variable = undefined
- variable = false; (false) typeof variable = boolean
- variable = 0; (false) typeof variable = number
- variable = NaN; (false) typeof variable = number
Empty String Test:
if (variable === '')
- variable = ''; (true) typeof variable = string
- variable = null; (false) typeof variable = object
- variable = undefined; (false) typeof variable = undefined
- variable = false; (false) typeof variable = boolean
- variable = 0; (false) typeof variable = number
- variable = NaN; (false) typeof variable = number
Undefined Test:
if (typeof variable == "undefined")
-- or --
if (variable === undefined)
- variable = ''; (false) typeof variable = string
- variable = null; (false) typeof variable = object
- variable = undefined; (true) typeof variable = undefined
- variable = false; (false) typeof variable = boolean
- variable = 0; (false) typeof variable = number
- variable = NaN; (false) typeof variable = number
False Test:
if (variable === false)
- variable = ''; (false) typeof variable = string
- variable = null; (false) typeof variable = object
- variable = undefined; (false) typeof variable = undefined
- variable = false; (true) typeof variable = boolean
- variable = 0; (false) typeof variable = number
- variable = NaN; (false) typeof variable = number
Zero Test:
if (variable === 0)
- variable = ''; (false) typeof variable = string
- variable = null; (false) typeof variable = object
- variable = undefined; (false) typeof variable = undefined
- variable = false; (false) typeof variable = boolean
- variable = 0; (true) typeof variable = number
- variable = NaN; (false) typeof variable = number
NaN Test:
if (typeof variable == 'number' && !parseFloat(variable) && variable !== 0)
-- or --
if (isNaN(variable))
- variable = ''; (false) typeof variable = string
- variable = null; (false) typeof variable = object
- variable = undefined; (false) typeof variable = undefined
- variable = false; (false) typeof variable = boolean
- variable = 0; (false) typeof variable = number
- variable = NaN; (true) typeof variable = number
As you can see, it's a little more difficult to test against NaN;