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

Discussions

[request] is null a number?
Depends entirely on how you want to define number. Our notion of that has evolved over the millennia, and ultimately a number is just a concept that we use in math for convenience. If you have 20 minutes to spare watch this suckerpinch video . It's tongue in cheek but he does an amazing job of creating a meaningful application using NaN (Not a Number) as a number. Meaningful obviously is a bit subjective here, so to clarify I use that in the informal sense to distinguish the legit math from the bullshit numerology. More on reddit.com
🌐 r/theydidthemath
7
0
February 5, 2021
Why "null" in number question?
This may be a silly question, but why do decimal and number question types return the value "null" if nothing is filled in? Should'nt it be a number, i.e. 0 (zero)? In my case it mess up copy and paste in a spreadsheet if I formatted cells to currency etc. It means I can't paste the whole range ... More on community.kobotoolbox.org
🌐 community.kobotoolbox.org
0
0
November 2, 2011
soft question - In mathematics is null == zero? - Mathematics Stack Exchange
The distinction between the empty set $\emptyset$ and the number $0$ is similar to that between NULL and ZERO. For example, the set of real solutions (or informally "the solution") to $x^2=-1$ is $\emptyset$, but the solution to $x^2=0$ is $0$. More on math.stackexchange.com
🌐 math.stackexchange.com
javascript - Why `null >= 0 && null <= 0` but not `null == 0`?
If hint is "default", let hint be "number". ... The allowed values for hint are "default", "number", and "string". Date objects, are unique among built-in ECMAScript object in that they treat "default" as being equivalent to "string". All other built-in ECMAScript objects treat "default" as being equivalent to "number". (ECMAScript 20.3.4.45) So I think null converts to 0. ... I had the same problem !!. Currently my only solution ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Biztory
biztory.com β€Ί home β€Ί blog β€Ί 7 things you should know about null values
7 Things to know about NULL values - Biztory | Biztory
October 25, 2025 - In a world of fill-out forms and ... In practice, the most common solution for this particular problem is to mark those missing values as NULL....
🌐
Reddit
reddit.com β€Ί r/theydidthemath β€Ί [request] is null a number?
r/theydidthemath on Reddit: [request] is null a number?
February 5, 2021 - Meaningful obviously is a bit ... to the dictionary, null is "a zero" or "having or associated with the value zero." so yes, it is 0....
🌐
KoboToolbox
community.kobotoolbox.org β€Ί user support
Why "null" in number question? - User Support - KoboToolbox Community Forum
November 2, 2011 - This may be a silly question, but why do decimal and number question types return the value "null" if nothing is filled in? Should'nt it be a number, i.e. 0 (zero)? In my case it mess up copy and paste in a spreadsheet if I formatted cells to currency etc. It means I can't paste the whole range ...
🌐
Baeldung
baeldung.com β€Ί home β€Ί java β€Ί java numbers β€Ί check if an integer value is null or zero in java
Check if an Integer Value Is Null or Zero in Java | Baeldung
January 8, 2024 - As the code above shows, (num == null ? 0 : num) first checks if the num variable is null. If this is the case, we take the zero.
🌐
Microsoft Power BI Community
community.powerbi.com β€Ί t5 β€Ί Power-Query β€Ί We-cannot-convert-the-value-null-to-type-Number β€Ί td-p β€Ί 2057876
Solved: We cannot convert the value null to type Number - Microsoft Power BI Community
September 5, 2021 - To overcome this you can wrap that statement in a try-otherwise error-handler: try Number.IsNaN([Sales history months]) otherwise true ... If you liked my solution, please give it a thumbs up.
Find elsewhere
🌐
W3Schools
w3schools.com β€Ί sql β€Ί sql_null_values.asp
SQL NULL Values - IS NULL and IS NOT NULL
ADD ADD CONSTRAINT ALL ALTER ALTER COLUMN ALTER TABLE ALTER VIEW AND ANY AS ASC BACKUP DATABASE BETWEEN CASE CHECK COLUMN CONSTRAINT CREATE CREATE DATABASE CREATE INDEX CREATE OR REPLACE VIEW CREATE TABLE CREATE PROCEDURE CREATE UNIQUE INDEX CREATE VIEW DATABASE DEFAULT DELETE DESC DISTINCT DROP DROP COLUMN DROP CONSTRAINT DROP DATABASE DROP DEFAULT DROP INDEX DROP TABLE DROP VIEW EXEC EXISTS FOREIGN KEY FROM FULL OUTER JOIN GROUP BY HAVING IN INDEX INNER JOIN INSERT INTO INSERT INTO SELECT IS NULL IS NOT NULL JOIN LEFT JOIN LIKE LIMIT NOT NOT NULL OR ORDER BY OUTER JOIN PRIMARY KEY PROCEDURE RIGHT JOIN ROWNUM SELECT SELECT DISTINCT SELECT INTO SELECT TOP SET TABLE TOP TRUNCATE TABLE UNION UNION ALL UNIQUE UPDATE VALUES VIEW WHERE MySQL Functions
🌐
Mimo
mimo.org β€Ί glossary β€Ί sql β€Ί is-not-null
SQL IS NOT NULL Condition: Syntax, Usage, and Examples
In SQL Server, you can use IS NOT NULL inside WHERE, CASE, JOIN, and HAVING clauses. The syntax is the same as in other databases: ... You can also pair it with the ISNULL() function if you want to display a placeholder when a value is missing but still filter the valid ones: ... SELECT name, ...
Top answer
1 of 6
254

Your real question seem to be:

Why:

null >= 0; // true

But:

null == 0; // false

What really happens is that the Greater-than-or-equal Operator (>=), performs type coercion (ToPrimitive), with a hint type of Number, actually all the relational operators have this behavior.

null is treated in a special way by the Equals Operator (==). In a brief, it only coerces to undefined:

null == null; // true
null == undefined; // true

Values false, '0', and [] are subject to numeric coercion to zero. Strings coerce to zero when, after trimming whitespace, the result is the empty string ("").

You can see the inner details of this process in the The Abstract Equality Comparison Algorithm and The Abstract Relational Comparison Algorithm.

In Summary:

  • Relational Comparison: When ToPrimitive(null, hint: Number) is called, and both values are not type String, ToNumber is called on both. This is the same as adding a + in front, which for null coerces to 0.

This also explains how Date objects can be compared numerically.

As for null >= 0, as ToPrimitive(null, hint: Number) results null.

Null The result equals the input argument (no conversion).

For the "The Abstract Relational Comparison Algorithm" of EcmaScript 5.1, this occurs in step 3.

EcmaScript 2025, 7.2.12 IsLessThan ( x, y, LeftFirst ), step 4 the same effect. (see: https://tc39.es/ecma262/#sec-islessthan)

c. NOTE: Because px and py are primitive values, evaluation order is not important. d. Let nx be ? ToNumeric(px). e. Let ny be ? ToNumeric(py).

  • Equality Comparison: only calls ToNumber on Strings, Numbers, and Booleans.

7.1.4 ToNumber ( argument ) The abstract operation ToNumber takes argument argument (an ECMAScript language value) and returns either a normal completion containing a Number or a throw completion. It converts argument to a value of type Number. It performs the following steps when called:

  1. If argument is a Number, return argument.
  2. If argument is either a Symbol or a BigInt, throw a TypeError exception.
  3. If argument is undefined, return NaN.
  4. If argument is either null or false, return +0𝔽.
  5. If argument is true, return 1𝔽.
  6. If argument is a String, return StringToNumber(argument).
  7. Assert: argument is an Object.
  8. Let primValue be ? ToPrimitive(argument, NUMBER).
  9. Assert: primValue is not an Object.
  10. Return ? ToNumber(primValue).
2 of 6
23

I'd like to extend the question to further improve visibility of the problem:

null >= 0; //true
null <= 0; //true
null == 0; //false
null > 0;  //false
null < 0;  //false

It just makes no sense. Like human languages, these things need be learned by heart.

🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί a-quick-and-thorough-guide-to-null-what-it-is-and-how-you-should-use-it-d170cea62840
A quick and thorough guide to β€˜null’: what it is, and how you should use it
June 12, 2018 - A default value is automatically assigned to s3. In case of a reference type, the default value is null. In case of a value type, the default value depends on the variable’s type. For example 0 for integer numbers, false for a boolean, and so on.
🌐
Tsql
tsql.nu β€Ί t-sql-code β€Ί what-is-null
What is NULL? – tsql.nu
October 31, 2019 - SELECT COUNT(*) as NotFiveGears_CORRECT ... alternative solution would be to use either ISNULL or COALESCE functions, to replace NULL values with something else, and then compare that with β€œnot 5”. This will however be problematic ...
🌐
Microsoft Learn
learn.microsoft.com β€Ί en-us β€Ί answers β€Ί questions β€Ί 911838 β€Ί sql-query-needs-to-handle-both-null-and-integer-va
Sql query needs to handle both NULL and INTEGER value. - Microsoft Q&A
July 2, 2022 - The result returned from the database is NULL but since i used IFNULL condition the final result is returned as 0. ... An Azure relational database service. ... A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions. ... 1) No number exist in VWNUMBERS => return NULL.
🌐
MDN Web Docs
developer.mozilla.org β€Ί en-US β€Ί docs β€Ί Web β€Ί JavaScript β€Ί Reference β€Ί Operators β€Ί null
null - JavaScript - MDN Web Docs
This is a bug in JavaScript that cannot be fixed due to backward compatibility. Unlike undefined, JSON.stringify() can represent null faithfully.
🌐
Medium
medium.com β€Ί @sumeet2703 β€Ί understanding-the-difference-between-zero-and-null-in-programming-db60b4e12934
Understanding the Difference Between Zero and Null in Programming | by Sumeet K | Medium
September 4, 2024 - Null is a special marker used to indicate the absence of value, particularly in reference types. ... Operations on zero generally succeed, as it is a valid number.
🌐
Iowa State University
instr.iastate.libguides.com β€Ί spreadsheets β€Ί zero
Zero and null values - Escape from Spreadsheet Hell - Library Guides at Iowa State University
October 3, 2024 - The website for the workshop! TL;DR we’ll teach you how to create spreadsheets optimized for reuse by you and others. Your future self will thank you. Zero is a number, null is not but what does your data say?
🌐
Reddit
reddit.com β€Ί r/sql β€Ί need some knowledge on null and not null
r/SQL on Reddit: Need some knowledge on NULL and NOT NULL
December 22, 2021 -
  • Where and why exactly a null is used?

  • What is exactly null and not null? To my understanding Not null we use when its mandatory to insert some value in that field, also when we give check constraint so by default the column will be not null right?

  • By adding new column through alter method default values are null, so how would I be able to insert values in it and is it right to give not null constraint to that new column while adding through alter method, basically when null and when not null to be used?...

god this is so confusing please help me, ik im asking alot but im really confused