JavaScript Quiz #05 — Type Conversion and Coercion
Topics: Type conversion and Type coercion
Checkout the interactive quiz https://quizzesforyou.com/quiz/jstypeconversion
Checkout All JavaScript Quizzes
Type conversion and Coercion are important concepts in JavaScript that deal with converting values from one type to another.
What is Type Coercion?
Coercion refers specifically to implicit type conversion, where JavaScript automatically converts the type of a value to match the type expected by an operation.
What is Explicit Type Conversion?
Explicit type conversion, also known as type casting, involves manually converting a value from one type to another.
A quick refresher on the concepts:
- Implicit Type Conversion (Coercion):
Coercion occurs when you perform operations between values of different types.
JavaScript tries to automatically convert the type of one value to match the other value so that the operation can proceed.
Coercion is commonly observed when using operators like ‘+’, `*`, or when using the loose equality operator (`==`).
Points to remember:
Concatenating a number and a string: When you add a number and a string using the `+` operator, JavaScript converts the number to a string and performs concatenation.
Multiplying a number with a string: When you multiply a number and a string using the `*` operator, JavaScript converts the string to a number before performing the multiplication.
Coercion can also occur with other operators and data types.
// Implicit Type Conversion (Coercion) Example
const result = 42 + " is the answer.";
console.log(result); // "42 is the answer."
console.log(typeof result); // "string"
2. Loose Equality Operator (==):
The loose equality operator checks if the values are equal, disregarding their types.
If the values have different types, coercion occurs before the comparison.
// Loose Equality Operator Example
const x = true;
const y = "1";
console.log(x == y); // true
x
is a boolean value (true
) and y
is a string value ("1"
), coercion occurs. The boolean value true
is coerced to a numeric value, which becomes 1
. Then the comparison takes place between 1
and "1"
.
During the comparison, JavaScript converts the string "1"
to a numeric value, which is also 1
. Now both values are of the same type (number
), and their values are equal (1
).
3. Strict Equality Operator (===):
The strict equality operator checks if the values are equal and also compares their types.
It does not perform any type coercion.
const x = true;
const y = "1";
console.log(x === y); // false
When using the strict equality operator (===
), both the values and types of the operands are compared without any coercion. In this case, x
is a boolean and y
is a string.
Since true
and "1"
have different types (boolean and string respectively), the strict equality comparison evaluates to false
, indicating that the values and types are not equal without type conversion.
4. Explicit Type Conversion (Type Casting):
Explicit type conversion involves manually converting a value from one type to another using type constructors or functions.
Type casting allows you to control the conversion process and ensure that the values have the desired types.
Common type constructors/functions used for type casting include `String()`, `Number()`, `Boolean()`, etc.
Use Cases for Explicit Type Conversion:
When you need to convert a value to a specific type for a particular operation or comparison.
For example, when working with data from an API, you might need to convert string values to numbers explicitly before performing numeric operations.
// Explicit Type Conversion (Type Casting) Example
const number = 3.14;
const integerNumber = parseInt(number);
console.log(integerNumber); // 3
console.log(typeof integerNumber); // "number"
It’s generally recommended to use the strict equality operator (`===`) for comparing values to avoid unexpected results caused by coercion. However, understanding coercion and being aware of its implications can be useful when working with JavaScript.
Remember to consider the context and requirements of your code to determine when and how to use type conversion or handle coercion effectively.
Checkout the interactive quiz https://quizzesforyou.com/quiz/jstypeconversion
- What is the output?
const a = 3;
const b = "5";
console.log(a + b);
a) 8
b) “8”
c) 35
Answer: c) 35
In this case, the +
operator performs string concatenation instead of numerical addition because one of the operands is a string. The resulting value is the concatenation of the string representation of a
with b
.
2. What is the output?
const x = "10" - 2;
console.log(x);
a) “10”-2
b) “8”
c) 8
Answer: c) 8
The -
operator performs numerical subtraction. Since "10"
can be converted to a number, the subtraction operation takes place, resulting in the numerical value of 8
.
3. What is the output?
const a = true;
const b = 2;
console.log(a + b);
a) 3
b) NaN
c) true2
Answer: a) 3
true
is coerced to 1
and the +
operator performs numerical addition. Therefore, a + b
results in 3
.
4. What is the output?
const x = "20" \* 4;
console.log(x);
a) 24
b) 80
c) NaN
Answer: b) 80
The *
operator performs numerical multiplication. The string "20"
is converted to a number and the multiplication operation takes place, resulting in the numerical value of 80
.
5. What is the output?
const a = null;
const b = 5;
console.log(a + b);
a) 5
b) null5
c) NaN
Answer: a) 5
when performing an arithmetic operation involving null and a numeric value, the null value is coerced to 0. Therefore, the expression a + b becomes 0 + 5, resulting in 5.
6. What is the output?
const a = "Hello";
const b = " World";
console.log(a - b);
a) NaN
b) “Hello World”
c) Undefined
Answer: a) NaN
The -
operator is not defined for strings, so the subtraction operation between strings results in NaN
(Not-a-Number).
7.What is the output?
const a = 0;
const b = false;
console.log(a == b);
a) true
b) false
c) Type Error
Answer: a) true
The loose equality operator (==
) performs type coercion. In this case, false
is converted to the number 0
, and then the comparison takes place, resulting in true
.
8. What is the output?
const x = null == undefined;
console.log(x);
a) true
b) false
c) Reference Error
Answer: a) true
The loose equality operator (==
) considers null
and undefined
as equal values, resulting in true
.
9. What is the output?
const x = \[\] == "";
console.log(x);
a) true
b) false
c) Error
Answer: a) true
The loose equality operator (==
) performs type coercion. In this case, the empty string ""
is converted to an empty array []
, and then the comparison takes place, resulting in true
.
10. What is the output?
const x = "0" === 0;
console.log(x);
a) true
b) false
c) NaN
Answer: b) false
The strict equality operator (===
) does not perform type coercion. In this case, "0"
is a string and 0
is a number, so they are not considered equal.
Check more quizzes@ https://quizzesforyou.com/
References: https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion
Subscribe to my newsletter
Read articles from quizzesforyou directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by