Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Types of type conversions
3.
Implicit type conversion
4.
Explicit type conversion
5.
String conversion
6.
Numeric Conversion
7.
Numeric conversion rules
8.
Boolean Type Conversion
9.
Javascript Type Conversion Table
10.
Frequently Asked Questions
11.
Key Takeaways
Last Updated: Mar 27, 2024

Type Conversion in Javascript

Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

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.

Also See, Javascript hasOwnProperty

 

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.

Read More About, Basics of Javascript

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

For good practice, you can implement it on an online JS editor.

Numeric conversion rules

Value Becomes
null 0
true 1
false 0
undefined NaN
string White 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


Must Read Fibonacci Series in JavaScript

Javascript Type Conversion Table

Value String Conversion Number Conversion Boolean Conversion
1 "1" 1 true
0 "0" 0 false
"1" "1" 1 true
"0" "0" 0 true
"one" "one" NaN true
true "true" 1 true
false "false" 0 false
null "null" 0 false
undefined "undefined" NaN false
'' "" 0 false
' ' " " 0 true

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. Don't stop here. Check out our Basics of JavaScript - guided path to practise Javascript from scratch.

 

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. Don't stop here. Check out our Basics of JavaScript - guided path to practise JavaScript from scratch.

Previous article
Variables and Data types in Javascript
Next article
Operators in Javascript
Live masterclass