Coercion Flashcards
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
What is the result of
+[] ?
0
What is the result of
+‘abc’ ?
NaN
What data types can you call toString() on?
All data types except null and undefined.
how do you call toString() on a number literal?
(23).toString()
or
let number=23;
number.toString()
or
23..toString()
What is the result of
true.toString() & false.toString() ?
> true.toString()
‘true’
false.toString()
‘false’
What is the result of
> [1, null, 2, undefined, 3].toString()
‘1,,2,,3’
Array.prototype.toStringtreatsnullandundefinedelements as empty values
What is the result of
> let obj = {a: ‘foo’, b: ‘bar’}
> obj.toString()
‘[object Object]’
By default,Object.prototype.toStringreturns the string’[object Object]’
What is the result of
[1, 2, 3].toString()
‘1,2,3’
What is the result of
String(null)
‘null’
What is the result of
String(undefined)
‘undefined’
What is the result of
toString(null)?
This will throw a TypeError
What is the result of
toString(undefined)
This will throw a TypeError
What is the result of
String(42)
‘42’
What is the result of
‘1’ === 1?
false
What is the result of
‘1’ == 1?
true
The non-strict equality operator coerces the string’1’into a number and then compares it with the1on the right-hand side.
In what context is it ok to use implicit coercion?
- Don’t use
String()
ortoString()
inside${...}
expressions in template literals. - Feel free to use the unary
+
operator to convert strings to numbers.
What is the result of
1 == true?
true
What is the result of
3==true?
false
When comparing a boolean with any value,==coercestrueand
falseto their number equivalents, which are1and0
What is the result of
0==false?
true
What is the result of
‘1’ == true?
true
What is the result of
undefined == null?
true
if you have a value that may be null or undefined how can you test that in one step?
if (val == undefined) {
// do one thing
} else {
// do another thing
}
What happens when comparing objects with ==
When both operands are objects,==behaves exactly like the===operator; that is, it considers two objects equal only when they’re the same object:
when one of the operands is an object and the other a primitive,==coerces the object to a primitive before making the comparison
‘[object Object]’ == {}
true
What happens when a number is compared with a string using==
the string is coerced into a number
When a boolean is compared with any other value
it is coerced into a number and compared again using the==operator.
result: ‘number ‘ + 1
‘number 1’
whenever one of the operands of the+operator is a string, the other operand is also coerced to a string and concatenated with the string:
’’ + undefined
‘undefined’
’’ + true
‘true’
’’ + [1, 2, 3]
‘1,2,3’
’’ + {}
‘[object Object]’
1 + undefined;
// NaN
null + false;
// 0
1 + true;
2
[1] + 2;
12
[1, 2] + 3;
// “1,23”
[] + true;
“true”
How are The relational operators,<,>,<=, and>= defined
They are defined for numbers (numeric comparison) and strings (lexicographic order).
‘b’ > ‘a’
true
1 < 2
true
How does JS choose lexicographic or numeric comparison?
When both operands are strings, JavaScript compares them lexicographically. Otherwise, JavaScript converts both operands to numbers before comparing them.
11 > ‘9’;
// true – ‘9’ is coerced to 9
123 > ‘a’;
// false – ‘a’ is coerced to NaN; any comparison with NaN is false
true > null;
// true – becomes 1 > 0
undefined >= 1;
// false – becomes NaN >= 1
null <= false;
// true – becomes 0 <= 0
What is the output of
console.log([1, 2, 3] + [4, 5]);
1,2,34,5