Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is null considered zero and undefined not a number on arithmetic expressions?

Is null evaluated to 0 and undefined to NaN on arithmetic expressions?

According to some testing it seems so:

> null + null
0

> 4 + null
4

> undefined + undefined
NaN

> 4 + undefined
NaN

Is it safe or correct to assume this? (a quote from a documentation would be A+).

like image 302
talles Avatar asked Jan 08 '14 16:01

talles


People also ask

IS null considered as number?

In JavaScript, null is a special value that represents an empty or unknown value. For example, let number = null; The code above suggests that the number variable is empty at the moment and may have a value later.

Is undefined not a number?

The value of +undefined is not-a-number, but it's a number nonetheless (albeit with a special value) and therefore not undefined.

IS null same as undefined?

undefined and null variables oftentimes go hand-in-hand, and some use the terms interchangeably. Though, there is a difference between them: undefined is a variable that refers to something that doesn't exist, and the variable isn't defined to be anything. null is a variable that is defined but is missing a value.

What is the difference between null undefined and not defined?

null is an assigned value. It means nothing. undefined means a variable has been declared but not defined yet.


3 Answers

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

like image 82
Bergi Avatar answered Sep 23 '22 17:09

Bergi


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

like image 30
talles Avatar answered Sep 25 '22 17:09

talles


Without being type bound,

null == false == 0

null !== false !== 0

http://www.mapbender.org/JavaScript_pitfalls:_null,_false,_undefined,_NaN#0_6

With that said, null == 0, null + 4 = 4

I hope this helps.

like image 22
Churk Avatar answered Sep 25 '22 17:09

Churk