Type Conversion in Javascript (with Example)

In this tutorial, you will learn about type conversion in javascript with the help of multiple examples.

In programming, type conversion or type casting is the process of converting data from one type to another. For example, converting data from string to number.

In Javascript, there are two types of type conversion.

Implicit Type Conversion – automatic type conversion

Explicit Type Conversion – manual type conversion

Implicit Type Conversion in Javascript

In implicit type conversion, javascript automatically converts data from one type to another. We call it implicit type conversion because this whole process is done automatically by javascript.

Example 1: Implicit Boolean Conversion to String

// implicit boolean conversion to string
let result;

result = '2' + true;
console.log(result); // 2true

result = '2' + false;
console.log(result); // 2false

Example 2: Implicit Number Conversion to String

// implicit number conversion to string

let result;

result = '2' + 1;
console.log(result); // 2true

result = 1 + '2';
console.log(result); // 2false

Note: If we try to add a number to the string then instead of adding, it will convert the number into a string and then perform the concatenation.

Example 3: Implicit String Conversion to Number

// implicit string conversion to number

let result;

result = '5' - '1'; 
console.log(result); // 4

result = '5' - 2;
console.log(result); // 3

result = '5' * 2;
console.log(result); // 10

result = '4' / 2;
console.log(result); // 2

Example 4: Implicit Boolean Conversion to Number

// implicit boolean conversion to number

let result;

result = '3' - true;
console.log(result); // 2

result = 3 + true;
console.log(result); // 4

result = 2 + false;
console.log(result); // 2

Note: If we convert a boolean to a number then true will be converted to 1 and false will be converted to 0.

Example 5: Implicit null Conversion to Number

// implicit null conversion to number

let result;

result = 2 + null;
console.log(result);  // 2

result = 2 - null;
console.log(result);  // 2

result = null - 2;
console.log(result);  // -2

Note: In javascript null represents empty or you can also consider it 0. So if we do 2 + null which means 2 + 0 then the result will be 2.

Explicit Type Conversion in Javascript

In explicit type conversion, we manually need to convert data from one type to another type with the help of some javascript methods.

We call it explicit type conversion because this whole process is done by the programmer with the help of some built-in javascript methods.

1. Convert String to Number Explicitly

Here we are using the javascript Number() method to explicitly convert the string into a number. For example,

// string to number

let result;

result = Number('125');
console.log(result); // 125

result = Number('125e-1');
console.log(result); // 12.5

2. Convert Boolean to Number Explicitly

Here we are using the javascript Number() method to explicitly convert a boolean into a number. For example,

// boolean to number

let result;

result = Number(true);
console.log(result); // 1

result = Number(false);
console.log(result); // 0

3. Convert null and empty string to Number Explicitly

In javascript, if we try to convert null and empty string " " into a number then we will get 0 as a result.

// null and empty string to Number

let result;

result = Number(null);
console.log(result); // 0

result = Number(" ");
console.log(result); // 0

4. Convert Number to String Explicitly

Here we are using the javascript String() and toString() methods to explicitly convert numbers into strings. For example,

// number to string

let result;

// using String()
result = String(124);
console.log(result);  // "124"

result = String(2 + 3);
console.log(result); // "5"

// using toString()
result = (124).toString();
console.log(result); // "124"

result = (2 + 3).toString();
console.log(result); // "5"

Note: In javascript, String() and toString() method does the pretty much same job.

5. Convert Boolean to String Explicitly

Here we are using the javascript String() method to explicitly convert a boolean to a string. For example,

// boolean to string

let result;

result = String(true);
console.log(result);  // "true"

result = String(false);
console.log(result); // "false"

6. Convert to Boolean Explicitly

Here we are using the javascript Boolean() method to convert other datatypes to boolean. For example,

let result;

// returns true
result = Boolean("hello");
console.log(result);  // true

result = Boolean(1);
console.log(result); // true

result = Boolean(" ");
console.log(result); // true

// returns false
result = Boolean("");
console.log(result); // "false"

result = Boolean(0);
console.log(result); // false

result = Boolean(undefined);
console.log(result); // false

result = Boolean(null);
console.log(result); // false

result = Boolean(NaN);
console.log(result); // false

Leave a Comment

Your email address will not be published. Required fields are marked *