Coercion Flashcards
(71 cards)
Explicitly coerce the string ‘1’ to a number
let one = Number(‘1’)
What is the result of Number(‘cat’)?
NaN
What is the result of Number(‘’)?
0
What is the result of Number(‘ ‘)?
0
What is the result of Number({})?
NaN
What is the result of Number([])?
0
What is the result of Number([4])?
4
What is the result of Number([undefined])?
0
What is the result of Number([1,2,3])?
NaN
What is the result of Number(undefined)?
NaN
What is the result of Number(null)?
0
What is the result of Number(true)?
1
What is the result of Number(false)?
0
How is parseInt different than Number()?
parseInt() can be called on strings containing non-numeric chars after the number to parse.
parseIntalso accepts a second argument called theradix. It specifies the base of the number contained in the string. For example,10101in the binary numbering system (base-2) represents the number 21 in decimal (base-10).
> parseInt(‘10101’, 2)
21
What is the result of parseInt(‘12.52’)?
12
What is the result of parseInt(‘12oz’)?
12
What is the result of parseInt(‘+12oz’)?
12
What is the result of Number(‘12oz’)?
NaN
Does parseFloat accept a radix argument?
No
What is the result of parseFloat(‘12 grams’)?
12
What is the result of parseFloat(‘12.2 grams’)?
12.2
What is the result of
+”” ?
0
What is the result of
+‘1’ ?
1
What is the result of
+‘2.3’ ?
2.3