Coercion Flashcards

(71 cards)

1
Q

Explicitly coerce the string ‘1’ to a number

A

let one = Number(‘1’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the result of Number(‘cat’)?

A

NaN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the result of Number(‘’)?

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the result of Number(‘ ‘)?

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the result of Number({})?

A

NaN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the result of Number([])?

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the result of Number([4])?

A

4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the result of Number([undefined])?

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the result of Number([1,2,3])?

A

NaN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the result of Number(undefined)?

A

NaN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the result of Number(null)?

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the result of Number(true)?

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the result of Number(false)?

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How is parseInt different than Number()?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the result of parseInt(‘12.52’)?

A

12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the result of parseInt(‘12oz’)?

A

12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the result of parseInt(‘+12oz’)?

A

12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the result of Number(‘12oz’)?

A

NaN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Does parseFloat accept a radix argument?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the result of parseFloat(‘12 grams’)?

A

12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the result of parseFloat(‘12.2 grams’)?

A

12.2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the result of
+”” ?

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the result of
+‘1’ ?

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the result of
+‘2.3’ ?

A

2.3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is the result of +[] ?
0
26
What is the result of +'abc' ?
NaN
27
What data types can you call toString() on?
All data types except null and undefined.
28
how do you call toString() on a number literal?
(23).toString() or let number=23; number.toString() or 23..toString()
29
What is the result of true.toString() & false.toString() ?
> true.toString() 'true' > false.toString() 'false'
30
What is the result of > [1, null, 2, undefined, 3].toString()
'1,,2,,3' Array.prototype.toString treats null and undefined elements as empty values
31
What is the result of > let obj = {a: 'foo', b: 'bar'} > obj.toString()
'[object Object]' By default, Object.prototype.toString returns the string '[object Object]'
32
What is the result of [1, 2, 3].toString()
'1,2,3'
33
What is the result of String(null)
'null'
34
What is the result of String(undefined)
'undefined'
35
What is the result of toString(null)?
This will throw a TypeError
36
What is the result of toString(undefined)
This will throw a TypeError
37
What is the result of String(42)
'42'
38
What is the result of '1' === 1?
false
39
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 the 1 on the right-hand side.
40
In what context is it ok to use implicit coercion?
- Don't use `String()` or `toString()` inside `${...}` expressions in template literals. - Feel free to use the unary `+` operator to convert strings to numbers.
41
What is the result of 1 == true?
true
42
What is the result of 3==true?
false When comparing a boolean with any value, == coerces true and  false to their number equivalents, which are 1 and 0
43
What is the result of 0==false?
true
44
What is the result of '1' == true?
true
45
What is the result of undefined == null?
true
46
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 }
47
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
48
'[object Object]' == {}
true
49
What happens when a number is compared with a string using ==
the string is coerced into a number
50
When a boolean is compared with any other value
it is coerced into a number and compared again using the == operator.
51
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:
52
'' + undefined
'undefined'
53
'' + true
'true'
54
'' + [1, 2, 3]
'1,2,3'
55
'' + {}
'[object Object]'
56
1 + undefined;
// NaN
57
null + false;
// 0
58
1 + true;
2
59
[1] + 2;
12
60
[1, 2] + 3;
// "1,23"
61
[] + true;
"true"
62
How are The relational operators, <, >, <=, and >= defined
They are defined for numbers (numeric comparison) and strings (lexicographic order).
63
'b' > 'a'
true
64
1 < 2
true
65
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.
66
11 > '9';
// true -- '9' is coerced to 9
67
123 > 'a';
// false -- 'a' is coerced to NaN; any comparison with NaN is false
68
true > null;
// true -- becomes 1 > 0
69
undefined >= 1;
// false -- becomes NaN >= 1
70
null <= false;
// true -- becomes 0 <= 0
71
What is the output of console.log([1, 2, 3] + [4, 5]);
1,2,34,5