DEV Community

Cover image for Javascript Type Conversion
pavanbaddi
pavanbaddi

Posted on • Originally published at thecodelearners.com

Javascript Type Conversion

Using type Conversion we can change the datatype of one variable to another variable.

Converting Number to String


let salary = 25000.00;
document.write(salary.toString()); 

//Output
25000

Converting String to Number

We can use built-in methods to convert strings to numbers. The methods are Number(), parseFloat(), parseInt() and we'll also be using type of statement to check its datatype.

Using Number() objects


let x = "5000.25";
let num = Number(x);
document.write(Number(num));
document.write("<br>");
document.write(typeof num);

//Output
5000.25
number

Using parseFloat() method


let x = "5000.25";
let num = parseFloat(x);
document.write(num);
document.write("<br>");
document.write(typeof num);

//Output
5000.25
number

Using parseInt() method


let x = "5000.25";
let num = parseInt(x);
document.write(num);
document.write("<br>");
document.write(typeof num);

//Output
5000
number

As you can see in parseInt() method the decimal points are omitted this is because the num variable is of type integer and does not have decimal points.

Complete article in posted on thecodelearners javascript type conversion

Top comments (0)