← 목록

Type Conversion & Type Coercion in JavaScript

devto 2026-06-02 원문 보기 ↗


JavaScript Type Conversion

In JavaScript Type Conversion can be defined as converting the data type of the variables from one type to the other manually by the programmer(explicitly) or automatically by the JavaScript(implicitly).

Implicit Type Conversion (Coercion)
In JavaScript, the implicit type conversion or coercion conversion can be defined as the automatic conversion of the data type of the variables from one type to another type. Implicit type conversion mostly occurs when we are performing the arithmetic or the logical operations.

[Type coercion happens when you perform an operation on different data types, and the JavaScript engine tries to make them "fit" together.]

String with Number (Concatenation)

When we add a string with the number, the JavaScript automatically converts the number into a string and performs string concatenation.

let res= "15"+5;
console.log(res); [The output will be 20.]

Boolean to Number

Equality Comparison (==)

When we use the equality operator in the JavaScript, it compares them after converting the value into the same data type.
[Use === (Strict Equality): This checks both value and type without coercion, preventing weird results.]

Automatic Conversion in Logical Operations

In JavaScript, these values are automatically converts undefined, null, "" (empty string), false, NaN, and 0 to false, and all other values to true.

Explicit Type Conversion

In JavaScript, explicit type conversion can be defined as when we manually change the data type of the variable from one to other using some built-in JavaScript functions. JavaScript provides us the built-in functions for performing the explicit conversion.

Converting Strings to Numbers

The global method Number() converts a variable (or a value) into a number.

Converting Numbers to Strings

Converting to Boolean

In JavaScript, we can convert the value into a boolean we can use the Boolean() function

Reasons to Learn JavaScript Type Conversion