Why do both [-[]] and [+[]] return [0] in Javascript?
console.log([+[]])
console.log([-[]])
Asked
Active
Viewed 35 times
0

Donald Shahini
- 835
- 9
- 20
1 Answers
3
unary +
or -
operator just converts it operand to a number. Empty array converted to number is 0. So you got the same result for both [+[]]
and [-[]]
.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus
https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-unary-plus-operator

konclave
- 648
- 1
- 7
- 19