Type Conversion in Javascript

Ranjul Arumadi
Last Updated: May 13, 2022

Introduction

Type conversion in Javascript is the process of converting data of one type to another. There are two types of type conversion in Javascript. They are Implicit conversion and explicit conversion.

 

Some examples of type conversion are converting from String data to Number, Number to String, etc.

 

In this blog, we are going to look into Type Conversion in Javascript in detail.

Types of type conversions

Type conversion in Javascript is of two types. They are implicit conversion and explicit conversion.

 

  • Implicit type conversion - This means that the conversion happens automatically

 

  • Explicit type conversion - This means that the conversion has to be done manually by the user.

Implicit type conversion

As discussed briefly above, implicit type conversion in javascript is those type conversions that happen automatically.

 

Automatically??. Let’s understand this through some examples.

 

Let us try adding two numeric values in Javascript.

 

Code:

let answer;
answer= 1 + 2;
console.log(answer) 

 

Output:

3

 

So far, so good. Now let’s try adding a numeric value and a string together.

 

Code:

let answer;
answer= 1 + 'Ninja';
console.log(answer)

 

Output:

1Ninja

 

We do not get any error but get the output as a string. We can confirm this by using the typeof operator in Javascript.

 

Code:

let answer;
answer = 1 + 'Ninja';
console.log(answer);
console.log(typeof(answer));

 

Output:

1Ninja
string

 

What is happening here is that when we try to add a string and number, obviously, addition cannot be done there. So concatenation takes place there. In this situation, Javascript automatically converted the numeric value to a string to let the concatenation happen.

Explicit type conversion

We have come across the point that explicit type conversion in javascript is those conversions that must be done manually. Let us understand what this means through an example.

 

Imagine we need to convert a boolean value or a numeric value to a string. How can we do this? Let’s see.

 

Code:

let number='777';
let answer;
// Type conversion from String to number
console.log(number);
console.log(typeof(number)); 

// Using Number() function to do the conversion
answer = Number(number);
console.log(answer);
console.log(typeof(answer));

 

Output:

777
string
777
number

 

Here we have converted the string value to a number using the Number() function. Since we had to do this conversion for our own requirement, we call these explicit type conversions. 

String conversion

We do string conversion when we need the string form of a value. We use the String() function to convert a value to a string.

 

Code:

let input = false;
console.log("input = "+input);
console.log(typeof(input));
input = String(input);
console.log("input = "+input);
console.log(typeof(input));

 

Output:

input = false
boolean
input = false
string

Numeric Conversion

Numeric conversion can happen implicitly when we try to do mathematical operations on strings. We can also do numeric conversions explicitly.

 

Code showing implicit conversion:

let a = "9";
let b = "3";
console.log(a/b);

 

Output:

3

 

Explicit conversions to numeric data types can be done using the Number() function.

 

Code showing explicit type conversion:

let a = "9";
console.log(a);
console.log(typeof(a));
a = Number(a);
console.log(a);
console.log(typeof(a));

 

Output:

9
string
9 
number

Numeric conversion rules

ValueBecomes
null0
true1
false0
undefinedNaN
stringWhite spaces are stripped off. If the resulting string is empty, then the answer is 0. Otherwise, the number is read from the string. Errors give NaN.

 

Example code:

console.log(Number("777"));
console.log(Number("404ninja")); 
console.log(Number(true)); 
console.log(Number(false)); 

 

Output:

777
NaN
1
0

Boolean Type Conversion

Boolean type conversion is a relatively simple conversion. We can use the Boolean() function to convert to boolean data type. 

 

We have to note that empty values like 0, empty strings, null, undefined, NaN, etc. become false. Other values become true.

 

Code:

console.log(Boolean(1));
console.log(Boolean(0));
console.log(Boolean("Ninja")); 
console.log(Boolean(""));
console.log(Boolean(NaN));
console.log(Boolean(null)); 

 

Output:

true
false
true
false
false
false

 

Javascript Type Conversion Table

ValueString ConversionNumber ConversionBoolean Conversion
1"1"1true
0"0"0false
"1""1"1true
"0""0"0true
"one""one"NaNtrue
true"true"1true
false"false"0false
null"null"0false
undefined"undefined"NaNfalse
''""0false
' '" "0true

Frequently Asked Questions

1. What are the ways to convert dates to strings?

Ans:- The String() global method can convert dates to strings. The method toString() also does the same thing. Use them as String(Date()) and Date().toString() respectively.

 

2. What is the use of the parseInt() method?

Ans:- The parseInt() method parses a string and returns an integer.

 

3. What purpose does the toPrecision() method serve?

Ans:- The toPrecision() method returns a string, with a number written with a specified length.

Key Takeaways

Type conversion in Javascript is the process of converting data of one type to another. Two types of type conversion are Implicit conversion and explicit conversion. Implicit conversions mean that the conversion happens automatically. An Explicit conversion means that the conversion has to be done manually by the user. We can use functions like Number(), String(), Boolean() to achieve explicit type conversion in javascript.

 

If you enjoyed reading this article about type conversion in javascript, check out 30 Javascript Intermediate Interview Questions in 2021: Part 2 and 10 Best JavaScript Certifications In 2021.

Was this article helpful ?
0 upvotes

Comments

No comments yet

Be the first to share what you think