Addition vs Unary Plus
2022年5月1日小于 1 分钟
Addition vs Unary Plus
Question
What does the code snippet to the right output by console.log
?
There is a difference between Addition Operator(+) and Unary plus operator(+), even though they use the same '+'.
Snippet
console.log(1 + 2)
console.log(1 + + 2)
console.log(1 + + + 2)
console.log(1 + '2')
console.log(1 + + '2')
console.log('1' + 2)
console.log('1' + + 2)
console.log(1 + true)
console.log(1 + + true)
console.log('1' + true)
console.log('1' + + true)
console.log(1 + null)
console.log(1 + + null)
console.log('1' + null)
console.log('1' + + null)
console.log(1 + undefined)
console.log(1 + + undefined)
console.log('1' + undefined)
console.log('1' + + undefined)
console.log('1' + + + undefined)