JavaScript

What is Type Coercion in JavaScript ?

JavaScript provides different data types to store data, such as numbers, booleans, strings, etc. The conversion of one data type value to another is known as type coercion in JavaScript. Generally, type coercion is divided into two categories: implicit and explicit.

In implicit type coercion, the value of a data type is transformed into other data types without any interference. In explicit coercion, the conversion of one data type into another data type by the developer interference.

This post provides detailed information on the type coercion in JavaScript with the following learning outcomes:

How Does Type Coercion Work in JavaScript?

As discussed above, the conversion in the implicit coercion is carried out automatically, whereas the explicit type of coercion is assisted by the developers. The following conversion falls under the type of coercion phenomenon:

  • To string conversion
  • To Boolean conversion
  • To number conversion

In the upcoming sections, these conversions are explained with examples.

How to Convert Any Datatype to String Using Type Coercion in JavaScript

Using type coercion, you can convert any data type to a string type. A set of examples is provided to convert various data types to strings via type coercion.

Example 1: Converting Number to String Using Implicit Coercion in JavaScript

An example is demonstrated to convert the data type of one value to another data type by utilizing type coercion. The code is given below:

Code

var a = 10;

console.log("DataType before coercion: " + typeof a);

var a = a + '';

console.log("DataType after coercion: " + typeof a);

In the above code:

  • A number is initialized and its type is printed before coercion.
  • The numeric value is added as 10.
  • Again, the type of the variable is obtained using the type of operator.

Output

The output represents that the data type before coercion was “number”. After the coercion, the data type is changed to “string”.

Example 2: Converting Number to String Using Explicit Coercion

An example is provided to convert the data type of one value to another data type by utilizing explicit type coercion. For this purpose, the code of explicit type coercion is as follows.

Code

var a= 10;

console.log("Before coercion: " + typeof a)

var a = String(a);

console.log("After coercion: " + typeof a);

In the above code:

  • A variable is initialized as a number and its type is printed before coercion.
  • The string () method is applied to that variable for explicit coercion.
  • Lastly, the type of the variable is again retrieved after coercion.

Output

The output represents that the datatype is a “number” before the coercion. However, after coercion, the data type is transformed to “string”.

How to Convert Any Datatype to Boolean Via Type Coercion in JavaScript?

As in the above examples, the number-to-string conversion is carried out via implicit as well as explicit type coercion. The following example code enables you to understand the implicit/explicit type coercion from number to Boolean.

Code

var x = 10;

console.log("type of x before coercion: " + typeof x)

//explicit type coercion from number to boolean
console.log("type of x after coercion: " + typeof Boolean(x))

In the above code:

  • A variable x is initialized as a number and its type is printed before the coercion.
  • Boolean() method is applied to that variable for explicit coercion.
  • Lastly, the type of the variable is again retrieved after coercion.

Output

The output illustrates that the datatype is a “number” before the coercion. However, after coercion, the data type is converted to “boolean”.

How to Convert Any Datatype to Number Via Type Coercion in JavaScript?

The string-to-number transformation is carried out here. The following example code enables you to understand the implicit/explicit type coercion from string to number.

Code

let result1;

let result2;

result1 = '324';

console.log("type of x before coercion: " + typeof (result1))

result2 = Number(324);

console.log("type of x after coercion: " + typeof (result2))

In the above code:

  • A variable result1 is initialized as a string and its type is printed before coercion.
  • Number() method is applied to that variable for explicit coercion.
  • Lastly, the type of the variable is again retrieved after coercion.

Output

The output represents that the datatype is “string” before the coercion. However, after coercion, the data type is converted to “number”.

Conclusion

The type coercion is the phenomenon of converting any datatype to a string, boolean, or number. The type of coercion can be either explicit or implicit. The implicit is the coercion type, which is carried out automatically, while the explicit depends on the needs of the developer. This article gives a detailed description of the type coercion concept in JavaScript. We have illustrated the implicit/explicit coercion of various data types to string, boolean, and number.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.