100 Days of JavaScript Interview Prep: A Beginner’s Journey
In this article, we start from the ground up, covering essential topics such as variables, data types, operators, control flow and common interesting topics every day.
DAY 1 – Implicit Type Coercion in JavaScript
100 Days of JavaScript Interview Prep: A Beginner’s Journey
Each concept is explained in clear, easy-to-understand language, accompanied by illustrative code examples to reinforce learning.
Get ready to ace your next JavaScript interview with this comprehensive guide! Join us as we document a 100-day adventure filled with learning, practice, and growth. In this series, we’ll take you through each day of preparation, starting from the basics and gradually progressing to more advanced concepts.
Question 1:
What is implicit type coercion in JavaScript, and can you provide an example?
Implicit type coercion in JavaScript refers an automatic conversion of values from one data type to another during operation. This usually happen when operands of different types involved in an operation.
Example with “+” operator with different types
JavaScript Example
let x = 10;
let y = '15';
console.log(x + y); // Output: "1015"
OUT PUT
1015
Another’s with “+” Operator
let x = 5;
let y = '10';
console.log(x + y); // Output: "510"
Conclusion (+ Operator)
In the above example + Operator, even though x
is a number and y
is a string, JavaScript implicitly coerces x
into a string and concatenates it with y
, resulting in the string "zz”
.
Subtraction Operators
let x = '10';
let y = 5;
console.log(x - y); // Output: 5
Explanation:
When the subtraction operator (-
) is applied, JavaScript automatically converts the string '10'
into a number and performs the subtraction operation with y
, resulting in 5
.
let x = 5;
let y = '10';
console.log(x - y); // Output: -5
Explanation:
When the subtraction operator (-
) is applied, JavaScript automatically converts the string '10'
into a number and performs the subtraction operation with x
, resulting in –5
.
Multiplication Operator (*)
let x = '5';
let y = '2';
console.log(x * y);
// Output: 10
Explanation:
Here, both x
and y
are strings representing numbers. JavaScript implicitly converts them into numbers and performs the multiplication operation, resulting in 10
.
Division Operator (/)
let x = '10';
let y = 3;
console.log(x % y);
// Output: 1
Explanation:
The modulus operator (%
) calculates the remainder of x
divided by y
. JavaScript coerces the string '10'
into a number and performs the modulus operation with y
, resulting in 1
.
Comparison Operators (== and !=):
console.log(5 == '5'); // Output: true
console.log(5 != '5'); // Output: false
Explanation:
JavaScript performs type coercion when using the loose equality operator (==
).
In this case, the string '5'
is coerced into a number, and since 5
is equal to 5
, the expression evaluates to true
.
Similarly, 5
is not not equal to '5'
, so the expression evaluates to false
.
Understanding implicit type coercion is important in JavaScript to avoid unexpected behavior and bugs in your code. It’s essential to be aware of how JavaScript handles data types during operations to write robust and reliable code.
If you find my blogs helpful, then please subscribe ,Claps and follow to support me in GitHub too.