Coercion Flashcards

0
Q

Coercion:

Does 2 + “3” == “2” + 3?

A

Yes. JavaScript always favors the string when combining a string and a number.

All other math operators /, *, %, - convert the string to a number.

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

Coercion:

Is 3 + true a valid expression in JavaScript?

A

Yes. It equals 4 because ‘true’ is converted to 1 by the JavaScript engine.

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

Coercion:

Does 3 + 4 + “5” = 3 + “4” + 5?

A

No. Because JavaScript evaluates left to right.

33 != 345

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

Coercion:

When null is converted to a number what is value?

A

This can mask errors!

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

Coercion:

When undefined is converted to a number what is its value?

A

NaN.

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

Coercion:

Is the statement

var x = NaN;
x == NaN;

True or false?

A

False. NaN by definition is never equal to itself.

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

Coercion:

If and object has both a valueOf and a toString method defined. What should the toString return, the type of the object or the string representation of the value?

A

The string representation of the value. The avoids errors with implicit conversion.

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

Coercion:

What is a simple way to avoid implicit coercion and explicitly convert a value to a number?

A

Use the + operator.

+“123” === 123
+form.date.value === today.getDate;

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

Coercion:
True/False

The == operator uses implied conversion and the === does not.

A

True.

== attempts to coerce the values into similar type before comparison. It is usually preferable to use the === operator.

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