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)

This post blew up on DEV in 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!