Coercion Flashcards

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
Q

What is the result of
+[] ?

A

0

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

What is the result of
+‘abc’ ?

A

NaN

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

What data types can you call toString() on?

A

All data types except null and undefined.

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

how do you call toString() on a number literal?

A

(23).toString()
or

let number=23;
number.toString()
or

23..toString()

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

What is the result of
true.toString() & false.toString() ?

A

> true.toString()
‘true’
false.toString()
‘false’

30
Q

What is the result of
> [1, null, 2, undefined, 3].toString()

A

‘1,,2,,3’
Array.prototype.toStringtreatsnullandundefinedelements as empty values

31
Q

What is the result of
> let obj = {a: ‘foo’, b: ‘bar’}
> obj.toString()

A

‘[object Object]’

By default,Object.prototype.toStringreturns the string’[object Object]’

32
Q

What is the result of
[1, 2, 3].toString()

A

‘1,2,3’

33
Q

What is the result of
String(null)

A

‘null’

34
Q

What is the result of
String(undefined)

A

‘undefined’

35
Q

What is the result of
toString(null)?

A

This will throw a TypeError

36
Q

What is the result of
toString(undefined)

A

This will throw a TypeError

37
Q

What is the result of
String(42)

A

‘42’

38
Q

What is the result of
‘1’ === 1?

A

false

39
Q

What is the result of
‘1’ == 1?

A

true
The non-strict equality operator coerces the string’1’into a number and then compares it with the1on the right-hand side.

40
Q

In what context is it ok to use implicit coercion?

A
  • Don’t useString()ortoString()inside${...}expressions in template literals.
  • Feel free to use the unary+operator to convert strings to numbers.
41
Q

What is the result of
1 == true?

A

true

42
Q

What is the result of
3==true?

A

false

When comparing a boolean with any value,==coercestrueand
falseto their number equivalents, which are1and0

43
Q

What is the result of
0==false?

A

true

44
Q

What is the result of
‘1’ == true?

A

true

45
Q

What is the result of
undefined == null?

A

true

46
Q

if you have a value that may be null or undefined how can you test that in one step?

A

if (val == undefined) {
// do one thing
} else {
// do another thing
}

47
Q

What happens when comparing objects with ==

A

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
Q

‘[object Object]’ == {}

A

true

49
Q

What happens when a number is compared with a string using==

A

the string is coerced into a number

50
Q

When a boolean is compared with any other value

A

it is coerced into a number and compared again using the==operator.

51
Q

result: ‘number ‘ + 1

A

‘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
Q

’’ + undefined

A

‘undefined’

53
Q

’’ + true

A

‘true’

54
Q

’’ + [1, 2, 3]

A

‘1,2,3’

55
Q

’’ + {}

A

‘[object Object]’

56
Q

1 + undefined;

A

// NaN

57
Q

null + false;

A

// 0

58
Q

1 + true;

A

2

59
Q

[1] + 2;

A

12

60
Q

[1, 2] + 3;

A

// “1,23”

61
Q

[] + true;

A

“true”

62
Q

How are The relational operators,<,>,<=, and>= defined

A

They are defined for numbers (numeric comparison) and strings (lexicographic order).

63
Q

‘b’ > ‘a’

A

true

64
Q

1 < 2

A

true

65
Q

How does JS choose lexicographic or numeric comparison?

A

When both operands are strings, JavaScript compares them lexicographically. Otherwise, JavaScript converts both operands to numbers before comparing them.

66
Q

11 > ‘9’;

A

// true – ‘9’ is coerced to 9

67
Q

123 > ‘a’;

A

// false – ‘a’ is coerced to NaN; any comparison with NaN is false

68
Q

true > null;

A

// true – becomes 1 > 0

69
Q

undefined >= 1;

A

// false – becomes NaN >= 1

70
Q

null <= false;

A

// true – becomes 0 <= 0

71
Q

What is the output of
console.log([1, 2, 3] + [4, 5]);

A

1,2,34,5