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)!

Answer from Bergi on Stack Overflow
Top answer
1 of 3
17

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)!

2 of 3
5

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

🌐
MDN Web Docs
developer.mozilla.org β€Ί en-US β€Ί docs β€Ί Web β€Ί JavaScript β€Ί Reference β€Ί Operators β€Ί null
null - JavaScript - MDN Web Docs
JavaScript is unique to have two nullish values: null and undefined. Semantically, their difference is very minor: undefined represents the absence of a value, while null represents the absence of an object.
Discussions

Why is `typeof null === 'object'` in JavaScript? The 30-year story of a bug we can't fix
I don't know if I am conditioned to it now, but the null as an object, NaN as number, Nan !== NaN has started making sense to me. Therefore, I welcomed the nullish coalescing operator instead of the short-circuiting or operator. More on reddit.com
🌐 r/javascript
67
138
October 14, 2025
javascript - Why is isNaN(null) == false in JS? - Stack Overflow
I adore JavaScript. 2013-07-03T08:42:52.327Z+00:00 ... Yes. I meant to convey that Number('abcd') is NaN but I implied that it tests true for equality, which is not the case. I will edit it. 2013-07-31T17:25:01.473Z+00:00 ... The conversion of null to 0 only (at least in this context) occurs ... More on stackoverflow.com
🌐 stackoverflow.com
How do I check for null values in JavaScript? - Stack Overflow
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 ... More on stackoverflow.com
🌐 stackoverflow.com
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.com
🌐 r/javascript
78
60
November 17, 2016
🌐
Untitled Publication
pranaygoel.hashnode.dev β€Ί demystifying-null
Demystifying null in Javascript. Why is null >= 0 equal to true?
August 14, 2023 - Type conversion is similar to type ... whereas type conversion can be either implicit or explicit. In Javascript, null is converted to undefined and undefined is converted to NaN, i.e., not-a-number which actually is of type ...
🌐
TutorialsPoint
tutorialspoint.com β€Ί How-null-is-converted-to-Number-in-JavaScript
How null is converted to Number in JavaScript?
If the value is not convertible, then it will return NaN. To convert the null into a Number we pass the "null" as an argument to the Number() method.
🌐
Programiz
programiz.com β€Ί javascript β€Ί null-undefined
JavaScript null and undefined
In JavaScript, null is a special value that represents an empty or unknown value. For example, ... The code above suggests that the number variable is empty at the moment and may have a value later.
🌐
Medium
medium.com β€Ί @maxwellarmah01 β€Ί understanding-javascript-comparisons-why-0-is-null-and-not-null-372d84d65284
Understanding JavaScript Comparisons: Why 0 is null and not null | by Maxwell Armah | Medium
June 10, 2024 - Here’s how this process unfolds: 1. Type Conversion for Relational Operators: β€” JavaScript converts both operands to numbers before making the comparison. β€” `null` is converted to `0` because `Number(null)` is `0`.
🌐
The Art of Code
artofcode.wordpress.com β€Ί 2017 β€Ί 03 β€Ί 21 β€Ί javascript-comparing-numbers-and-null
JavaScript, comparing numbers and null | The Art of Code
March 23, 2017 - Total nonsense! null is not less than 0 and is not equal to 0 but… it’s less or equal to 0. ... The conclusion is that you must be very cautious with null value in JavaScript. It should not be compared with numbers as results are against the logic.
Find elsewhere
🌐
Dmitri Pavlutin
dmitripavlutin.com β€Ί javascript-null
Everything about null in JavaScript - Dmitri Pavlutin
September 21, 2020 - null, alongside false, 0, '', undefined, NaN, is a falsy value. If a falsy value is encountered in conditionals, then JavaScript coerces falsy to false. ... typeof value operator determines the type of value.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί javascript β€Ί javascript-convert-string-number-to-a-number-or-null-return-0-for-0
JavaScript - Convert String/Number to a Number or Null, Return 0 for 0 - GeeksforGeeks
August 5, 2025 - In JavaScript, you may need to ... to a number or null, return 0 for 0. The Number() function is a built-in JavaScript method that converts a string or number to a number....
🌐
Medium
medium.com β€Ί @gajjar.parth1811 β€Ί null-behavior-comparison-in-javascript-20601d2e89a0
NULL behavior comparison in javascript | by Gajjar Parth | Medium
March 20, 2023 - In fact, the less than or greater than operators cannot be used to compare null and 0 at all! This is because the less than or greater than operators require that both operands be numbers, but null is not a number.
🌐
CodeBurst
codeburst.io β€Ί understanding-null-undefined-and-nan-b603cb74b44c
Understanding null, undefined and NaN. | by Kuba Michalski | codeburst
November 12, 2018 - NaN (Not-A-Number ) represents something which is not a number, even though it’s actually a number. It’s not equal to itself and to check if something is NaN we need to use isNaN() function. All of the above are falsy values so they convert to false. JavaScript loves converting values so you need to use triple equality signs (===) to make sure both elements are not the same. I hope this article was helpful for you and you’ve got some understanding what null, undefined and NaN are.
🌐
Reddit
reddit.com β€Ί r/javascript β€Ί why is `typeof null === 'object'` in javascript? the 30-year story of a bug we can't fix
r/javascript on Reddit: Why is `typeof null === 'object'` in JavaScript? The 30-year story of a bug we can't fix
October 14, 2025 - typeof NaN === "number", here number refers to JavaScript type, whereas the last N in NaN refers to IEEE754 number. ... In other words, assumption that "IEEE754 number" and "js number" are the same thing, is wrong. ... NaN always made sense if you know maths and IEEE 754. ... What's frustrating is that NaN ?? x is NaN, if I remember correctly. ... a ?? b only selects b when a == null (in the JS sense).
Top answer
1 of 10
136

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.

2 of 10
46

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.

🌐
GeeksforGeeks
geeksforgeeks.org β€Ί null-in-javascript
Null in JavaScript | GeeksforGeeks
June 5, 2024 - In JavaScript, `null` indicates the deliberate absence of any object value. It's a primitive value that denotes the absence of a value or serves as a placeholder for an object that isn't present.
🌐
Mastering JS
masteringjs.io β€Ί tutorials β€Ί fundamentals β€Ί null
`null` in JavaScript - Mastering JS
December 2, 2020 - In JavaScript, null is a value that represents the intentional absence of any object value. It is technically a primitive type, although in some cases it behaves as an object.
🌐
DEV Community
dev.to β€Ί icncsx β€Ί js-explain-undefined-null-and-nan-2lka
JS / explain undefined, null, and NaN - DEV Community
May 28, 2020 - You will also frequently encounter null as a return value of a function. It's pretty context driven as to what that really means, so I'll leave it up to you to figure it out. Last but not least, there is NaN (not a number).
🌐
bitsofcode
bitsofco.de β€Ί javascript-typeof
"What's the typeof null?", and other confusing JavaScript Types | bitsofcode
February 21, 2017 - The type of NaN, which stands for Not a Number is, surprisingly, a number. The reason for this is, in computing, NaN is actually technically a numeric data type. However, it is a numeric data type whose value cannot be represented using actual ...
🌐
Medium
medium.com β€Ί hacktive-devs β€Ί null-is-not-an-object-8faa6d8d0257
Null is not an Object. Hi there! if you’re a bit familiar with… | by James Falola | Hacktive Devs | Medium
February 21, 2020 - In JavaScript, typeof null returns β€˜object’. Then you start to wonder β€œwhy?”, since null means β€œnothing”. As a developer, my instinct has always been β€œWell, everything is an object in JS” until I got to think, well, not everything ...
🌐
SheCodes
shecodes.io β€Ί athena β€Ί 142857-understanding-the-values-null-and-undefined-in-javascript
[JavaScript] - Understanding the values null and undefined in JavaScript
Learn the meaning of null and undefined in JavaScript and how they are used to represent the absence of a value. ... if I have a variable outside a function, do I need to redefine that variable inside the function? similarly, can use the same name for a variable if one is in a function?
Top answer
1 of 16
1093

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).

2 of 16
732

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;