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

🌐
TutorialsPoint
tutorialspoint.com β€Ί How-null-is-converted-to-Number-in-JavaScript
How null is converted to Number in JavaScript?
To convert the null into a number we use the OR operator with null and any number and the OR operator will return that Number.
Discussions

javascript - what does this "a: ?number = null;" mean? - Stack Overflow
In Facebook's React Native's Geolocation documentation I see this line of code: watchID: ?number = null; I am clueless about it's nature. Is this even javascript? More on stackoverflow.com
🌐 stackoverflow.com
arrays - JavaScript null and plus (+) operatior - Stack Overflow
I am trying to understand the core of JavaScript. I know it doesnt have much implementation value. If you dont want to answer, just leave it. However, I will appreciate if you could help to underst... More on stackoverflow.com
🌐 stackoverflow.com
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
jquery - JavaScript convert NULL to 0 - Stack Overflow
I'm using jQuery to get the height of an element. But if the element doesn't exist, the following code will return NULL: More on stackoverflow.com
🌐 stackoverflow.com
🌐
freeCodeCamp
forum.freecodecamp.org β€Ί t β€Ί why-on-earth-does-null-0-give-false-as-a-result β€Ί 173701
Why on earth does null == "0" give false as a result? - The freeCodeCamp Forum
February 10, 2018 - So, i am now totally confused , javascript says one thing and then does totally opposite , i went to articles and videos to read about == and === , they all said == makes values convert to number … So how on earth when Number(null) = 0 and ...
🌐
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 - function convertTernary(value) { let n = Number(value); return isNaN(n) ? null : n || 0; } console.log(convertTernary('123')); console.log(convertTernary('abc')); console.log(convertTernary('0')); console.log(convertTernary(0)); ... num || 0 uses the fact that 0 is falsy in JavaScript.
🌐
Java2s
java2s.com β€Ί Tutorials β€Ί Javascript β€Ί Data_Type β€Ί Number_Convert β€Ί Convert_null_value_to_0_with_Number_function_in_JavaScript.htm
Convert null value to 0 with Number function in JavaScript
The following code shows how to convert null value to 0 with Number function. <!-- w w w . ja va 2s. c o m--> <!DOCTYPE html> <html> <head> <script type="text/javascript"> var num = Number(null)
🌐
MDN Web Docs
developer.mozilla.org β€Ί en-US β€Ί docs β€Ί Web β€Ί JavaScript β€Ί Reference β€Ί Global_Objects β€Ί Number
Number - JavaScript - MDN Web Docs - Mozilla
July 12, 2026 - If the number is NaN or -0, it's returned as 0. The result is therefore always an integer (which is not -0) or Β±Infinity. Notably, when converted to integers, both undefined and null become 0, because undefined is converted to NaN, which also becomes 0. JavaScript has some lower-level functions ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί null-in-javascript
Null in JavaScript | GeeksforGeeks
June 5, 2024 - let number = null; console.log("Type of number is:" ,typeof number); This example describes the Null value in JavaScript. JavaScript Β·
Find elsewhere
🌐
Untitled Publication
pranaygoel.hashnode.dev β€Ί demystifying-null
Demystifying null in Javascript. Why is null >= 0 equal to true?
August 14, 2023 - This particular expression is tricky and evaluated in a very different way from the above one. Under the hood, the comparison operators- > < >= <= return a boolean value, hence all of these do an implicit type conversion to make the two operands of the same type, hence it converts null to a number; i.e., Number(null) which returns 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 - Or at least I was assuming all above comparing expressions will be evaluated to false. The reality is even worse! Results are depending on a number which is value of y variable. ... So it looks null is considered in JavaScript to be less than 3.
🌐
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 - This special handling ensures that `null` is only equal to `undefined` and not to `0`. Understanding these nuances is crucial for JavaScript developers to avoid unexpected bugs and to write more predictable code. Here are a few takeaways: Explicit Type Conversion: When in doubt, convert types explicitly using functions like `Number()`, `String()`, or `Boolean()` to avoid relying on implicit type conversions.
🌐
Mastering JS
masteringjs.io β€Ί tutorials β€Ί fundamentals β€Ί null
`null` in JavaScript - Mastering JS
December 2, 2020 - For the purposes of arithmetic operations, null behaves like 0. If you add, subtract, multiply, divide, or exponentiate null, JavaScript converts null to 0.
🌐
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.
🌐
Dmitri Pavlutin
dmitripavlutin.com β€Ί javascript-null
Everything about null in JavaScript - Dmitri Pavlutin
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.
🌐
JavaScript Tutorial
javascripttutorial.net β€Ί home β€Ί an essential guide to javascript null
An Essential Guide to JavaScript null
September 29, 2020 - Use the optional chaining operator (?.) to access the property of an object that may be null. Was this tutorial helpful ? Yes No Β· Send Cancel Β· Previously Β· How to Check if an Array Contains a Value in Javascript Β· What is JavaScript Β· Install a JavaScript Code Editor Β· Meet the Console Tab of Devtools Β· JavaScript Hello World Β· Syntax Β· Variables Β· Data Types Β· Number Β·
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,

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

🌐
CSS { In Real Life }
css-irl.info β€Ί handling-null-undefined-and-zero-values-in-javascript
CSS { In Real Life } | Handling Null, Undefined and Zero Values in JavaScript
November 8, 2023 - That way, daysSinceLastPost will be used, even if the value is 0, while our fallback will be used if it is null or undefined.