Javascript Coercion Quiz
Matan Kushner
http://etc.ch/QoKj
Go to the quiz page on your phone:
> '5' - 3
> '5' - 3 // 2
The "-" operator only does one thing between operands, subtraction
Subtraction leads to both operands being coerced to numbers
> Number('5') - Number(3) // 2
> 5 - 2 // 2
> '5' + 3
> '5' + 3 // 53
The "+" operator does both addition and string concatenation
Because one of the operands is a string, it takes the role of the string concatenation operator
'5'.toString() + 3.toString() // 53
'5' + '3' // 53
> '5' - '1'
> '5' - '1' // 4
The "-" operator only does one thing between values, subtraction
Subtraction leads to both operators being coerced to numbers
> Number('5') - Number('1') // 4
> 5 - 1 // 4
> '5' + + '5'
> '5' + + '5' // '55'
> '5' + Number('5') // '55'
The "+" unary operator converts a value to a number
Unary operators have a higher priority than other operators, so they will always be resolved first
Because one of the operands is a string, the first plus takes the role of the string concatenation operator
> '5' + 5.toString() // '55'
> '5' + 5 // '55'
> '5' + '5' // '55'
> 'foo' + + 'bar'
> 'foo' + + 'bar' // 'fooNaN'
> 'foo' + Number('bar') // 'fooNaN'
The "+" unary operator converts its operand to a number
If a value is unable to be coerced into a number, it becomes NaN (Not a Number)
NaN is a number value for unrepresentable.
Because one of the operands is a string, the first plus takes the role of the string concatenation operator
> 'foo' + NaN // 'fooNaN'
> 'foo' + NaN.toString() // 'fooNaN'
> 'foo' + 'NaN' // 'fooNaN'
> '5' + - '2'
> '5' + - '2' // '5-2'
> '5' + Number(-2) // '5-2'
The "-" unary operator converts its operand to a number and negates it
Because one of the operands is a string, the first plus takes the role of the string concatenation operator
> '5' + -2.toString() // '5-2'
> '5' + -2 // '5-2'
> '5' + '-2' // '5-2'
> '5' + - + - - - '2'
> '5' + - + - - - '2' // '52'
> '5' + - + - - -2 // '52'
The "-" unary operator converts its operand to a number and negates it
Unary operators have a higher priority than other operators, so they will always be resolved first
Because one of the operands is a string, the first plus takes the role of the string concatenation operator
> '5' + - + - 2 // '52'
> '5' + - + -2 // '52'
> '5' + - -2 // '52'
> '5' + 2 // '52'
> '5' + 2.toString() // '52'
> '5' + '2' // '52'
> '5' + x - x
> x = 3
> '5' + x – x // 50
Without any unary operators, operations are evaluated from left to right
Because one of the operands is a string, the first plus takes the role of the string concatenation operator
> x = 3
> '5' + 3 – 3 // 50
> '53' – 3 // 50
> '53'.toNumber() – 3 // 50
> 53 – 3 // 50
> '5' - x + x
> x = 3
> '5' - x + x // 5
Without any unary operators, operations are evaluated from left to right
Because one of the operands is a string, the first plus takes the role of the string concatenation operator
> x = 3
> '5' - 3 + 3 // 5
> '5'.toNumber() - 3 + 3 // 5
> 5 - 3 + 3 // 5
> 2 + 3 // 5
You Don't Know JS: Types & Grammar
Kyle Simpson
More fun JS coercion!
Javascript Coercion Quiz
By Matan Kushner
Javascript Coercion Quiz
- 729