JavaScript Type Conversion (original) (raw)

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.

**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.

JavaScript `

let res = 5 + "5";
console.log(res);

`

**Boolean to Number

When we perform the mathematical operations, then JavaScript automatically converts true to 1 and false to 0.

JavaScript `

let res = true + 1;
console.log(res);

`

Equality Comparison (==)

When we use the equality operator in the JavaScript, it compares them after converting the value into the same data type.

JavaScript `

let res = (5 == "5");
console.log(res);

`

**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.

JavaScript `

let res = Boolean("");
let res2 = Boolean("Hello");
console.log(res) console.log(res2)

`

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 to String

In JavaScript, we can convert the value into a string using the String() function, toString(), and by concatenating the empty string.

JavaScript `

let n = 123; let s1 = String(n);
let s2 = n.toString();
console.log(s1) console.log(s2)

`

**Converting to Number

In JavaScript, we can convert the value into a number using the Number() function, parseInt(), and parseFloat().

JavaScript `

let s = "123"; let n = Number(s);
let s1 = "12.34"; let n1 = parseFloat(s1);
console.log(n) console.log(n1)

`

**Converting to Boolean

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

JavaScript `

let str = "Hello"; let b1 = Boolean(str);
let emptyStr = ""; let b2 = Boolean(emptyStr);
console.log(b1) console.log(b2)

`

Reasons to Learn JavaScript Type Conversion

JavaScript Type Conversion Comparison

Conversion Type Implicit Conversion (Coercion) Explicit Conversion
String + Number Automatically converts numbers to strings Manually convert using String() or .toString()
Number + Boolean Converts true to 1 and false to 0 Use Number() to explicitly convert
String to Boolean Non-empty strings convert to true, empty to false Use Boolean() for clarity
Number from String Implicit coercion with + operator Use Number(), parseInt(), or parseFloat()