0

When i am trying to do

console.log("100" * null) // (1)
console.log("100" + null) // (2)

its giving me result "0" and "100null" respectively I just wanted to know how javascript typecasting working in this case .

CompuChip
  • 9,143
  • 4
  • 24
  • 48
Roli Agrawal
  • 2,356
  • 3
  • 23
  • 28

1 Answers1

1

In the first cases "100" is converted to 100, since * is a math operator that acts on Numbers.

All the operands are converted to Number

(+"100") * (+null) // +null = 0; 100 * 0 = 0

and in the second case, + isn't viewed as math operator, instead as concatenate operator(due to the first operand being String)

So null is converted to it's string representation due to the call to it's internal ToString method, so it's just concatenation("100" + "null")

Amit Joki
  • 58,320
  • 7
  • 77
  • 95