What is Type Coercion in JavaScript?

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Type Coercion in JavaScript

Type Coercion in JavaScript refers to the process of conversion of values from one data type to another automatically. There are many data types like string, number, boolean, etc, So it is essential to know how these data types are coerced from one data type to another. In this article, we will learn, "What is Coercion in JavaScript?"

What is Type Coercion in JavaScript?

Type coercion in JavaScript refers to the automatic conversion of values from one data type to another.

  1. Numeric to String Coercion: Numeric values are automatically coerced to strings when concatenated with strings. This can be useful for formatting output or generating dynamic strings. Example:

  2. String to Numeric Coercion: Strings containing numeric characters are automatically coerced to numbers when used in mathematical operations. This allows for arithmetic operations involving string representations of numbers.

    Example:

  3. Boolean to Number: In numerical contexts, boolean values (true and false) are automatically coerced to numbers (1 and 0 respectively). This allows for boolean values to be used in arithmetic operations.

    Example:

  4. Comparison Coercion: In JavaScript, comparison operators (== and ===) perform type coercion. The == operator coerces values to the same type before comparison, while === strictly compares values without coercion.

    Example:

Rule of Type Coercion

The rules governing type coercion in JavaScript include:

  1. Any numbers, boolean values are converted to strings in where strings are expected, such as concatenation with strings or when using string functions.
  2. Non-numeric values are converted to numbers in where numbers are required, such as arithmetic operations or comparisons.
  3. Values are converted to booleans where booleans are expected, such as logical operations or conditional statements.
  4. If a conversion is not possible, it results in NaN (Not-a-Number), typically in arithmetic operations involving non-numeric values.

Type Coercion for Objects

  • In JavaScript, objects can undergo type coercion when they are used in contexts that expect primitive values like strings or numbers.
  • This coercion typically involves invoking specific methods or performing conversions based on the object's internal representation.

Example

In the above example, we can see that the addition of the variable num and object myContainer is returned as 20, which is correct. It is because of the valueOf() method in the object 'myContainer' which returned 8. In case the value of the method is not present in the object, NaN is returned.

Example of Type Coercion

In this example, the variable num is assigned the string "5". When num is used in a mathematical operation (*), JavaScript implicitly coerces it to a numeric value, resulting in the numeric value 10 as the output. This demonstrates how JavaScript automatically converts a string containing numeric characters to a number when used in a numerical context.

Conclusion

Conclusion:

  1. Type coercion in JavaScript automatically converts values from one data type to another.
  2. It includes conversion from Number to String, Boolean to Number, String to Number, etc.
  3. In Javascript, we can also convert objects into primitive values.