20

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

talles
  • 14,356
  • 8
  • 45
  • 58
  • It depends on the other argument type too : `"A"+null` – Denys Séguret Jan 08 '14 at 16:35
  • 4
    [The spec.](http://www.ecma-international.org/ecma-262/5.1/#sec-11.6.1) – Pointy Jan 08 '14 at 16:35
  • 1
    @dystroy, yup but that would be a *string concatenation*, and not an *arithmetic expression*. – talles Jan 08 '14 at 16:35
  • @Pointy, I can find the spec, no problem. What I would like is a quote from it ;) – talles Jan 08 '14 at 16:36
  • 2
    It's safe like a juggler with 10 torches. If he knows exactly what he doing, then it's safe. – Luca Rainone Jan 08 '14 at 16:36
  • Also, see: http://stackoverflow.com/questions/801032/why-is-null-an-object-and-whats-the-difference-between-null-and-undefined – Diodeus - James MacFarlane Jan 08 '14 at 16:36
  • @talles I find the difference of dubious use as the difference is resolved dynamically depending on the values. – Denys Séguret Jan 08 '14 at 16:36
  • 1
    @talles it's exactly the same operator - `+` – Pointy Jan 08 '14 at 16:36
  • @talles that's a link straight to the description of the `+` operator – Pointy Jan 08 '14 at 16:37
  • 1
    @Pointy, Just because it's the same character, doesn't mean it's the same operator. `string + ` is string concatenation, while `int + ` is integer addition. – Brian S Jan 08 '14 at 16:42
  • 1
    @BrianS well yes obviously the *operations* are different; my point is that it's that process described in the spec that determines what an expression involving that operator *means*. The Expression syntax doesn't distinguish between numeric addition and string concatenation, in other words. – Pointy Jan 08 '14 at 16:48

3 Answers3

16

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:

It is not used by the equality operators, so null == 0 is false (and null !== 0 anyway)!

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
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

talles
  • 14,356
  • 8
  • 45
  • 58
  • 1
    However note that if the *other* operand to `+` is a string, then `ToString` is called and string concatenation is carried out. So `null + "hello"` is the string `"nullhello"`. – Pointy Jan 08 '14 at 16:49
1

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.

Churk
  • 4,556
  • 5
  • 22
  • 37
  • I think it's fair to say: please don't rely on null for standard operations. You'll get away with it in JS but it's really bad practice. – aaron-bond Jan 08 '14 at 16:38
  • I totally agree, it is strictly a non type bound language pitfall or advantage depending on how you look at it. – Churk Jan 08 '14 at 16:39
  • Yeah, it likely depends on where you start. If you get to JS from a more structured language, it's terrifying to think of using it that way. If you started with JS, you'd reach a structured language and wonder why you can't do all the useful things! – aaron-bond Jan 08 '14 at 16:42
  • While `false == 0` with the non-strict equality, neither `false` nor `0` are equal to `null`. Have you tried this? – Bergi Jan 08 '14 at 17:14
  • The link seems to be broken. – Stephen Turner Aug 02 '18 at 14:47