Simple Ways to Convert Strings and Numbers in JavaScript

Converting strings to numbers and numbers to strings in JavaScript is straightforward. I’ll show every method I use, explain what each one does, and when to pick one over the other. You’ll see four different ways to turn a string into a number, and two ways to turn a number into a string.

String to number (JavaScript String-Number Conversion)

  Simple Ways to Convert Strings and Numbers in JavaScript  1  

Method 1 – Number()

Use the built-in Number() to convert any numeric string into a number.   Simple Ways to Convert Strings and Numbers in JavaScript  2   Steps: Create a variable that holds a number in the form of a string. Pass that variable to Number(). Store the result in a new variable and print it.   Simple Ways to Convert Strings and Numbers in JavaScript  6     Simple Ways to Convert Strings and Numbers in JavaScript  4     Simple Ways to Convert Strings and Numbers in JavaScript  3  
const str = "45";
const num = Number(str);

console.log(num);        // 45
console.log(typeof num); // "number"
  Simple Ways to Convert Strings and Numbers in JavaScript  5   If you want some quick values to experiment with, generate simple random numbers and convert them.

Method 2 – parseInt()

Use parseInt() for whole numbers. It ignores the decimal part.   Simple Ways to Convert Strings and Numbers in JavaScript  7   Steps: Create a string with a decimal value. Call parseInt() with the string. Print the result.   Simple Ways to Convert Strings and Numbers in JavaScript  10     Simple Ways to Convert Strings and Numbers in JavaScript  9     Simple Ways to Convert Strings and Numbers in JavaScript  8  
const str = "56.67";
const num = parseInt(str);

console.log(num);        // 56
console.log(typeof num); // "number"
  Simple Ways to Convert Strings and Numbers in JavaScript  11  

Method 3 – parseFloat()

Use parseFloat() to keep the decimal part.   Simple Ways to Convert Strings and Numbers in JavaScript  13   Steps: Keep the same decimal string. Call parseFloat() with the string. Print the result.   Simple Ways to Convert Strings and Numbers in JavaScript  15     Simple Ways to Convert Strings and Numbers in JavaScript  14     Simple Ways to Convert Strings and Numbers in JavaScript  12  
const str = "56.67";
const num = parseFloat(str);

console.log(num);        // 56.67
console.log(typeof num); // "number"
  Simple Ways to Convert Strings and Numbers in JavaScript  16   Parsing here is about numbers. If you’re working with structured text, learn to parse data using JSON.

Method 4 – Unary plus (+)

The unary plus is a quick way to convert a numeric string into a number.   Simple Ways to Convert Strings and Numbers in JavaScript  17   Steps: Create a string that holds a number. Prefix the variable with a single plus sign. Store and print the result.   Simple Ways to Convert Strings and Numbers in JavaScript  21     Simple Ways to Convert Strings and Numbers in JavaScript  19     Simple Ways to Convert Strings and Numbers in JavaScript  18  
const str = "99";
const num = +str;

console.log(num);        // 99
console.log(typeof num); // "number"
  Simple Ways to Convert Strings and Numbers in JavaScript  20  

Number to string (JavaScript String-Number Conversion)

  Simple Ways to Convert Strings and Numbers in JavaScript  22  

Method 1 – String()

Use String() to turn a number into a string.   Simple Ways to Convert Strings and Numbers in JavaScript  23   Steps: Create a number. Pass that number to String(). Print the result.   Simple Ways to Convert Strings and Numbers in JavaScript  26     Simple Ways to Convert Strings and Numbers in JavaScript  25     Simple Ways to Convert Strings and Numbers in JavaScript  24  
const num = 123;
const str = String(num);

console.log(str);        // "123"
console.log(typeof str); // "string"
  Simple Ways to Convert Strings and Numbers in JavaScript  27   When you serialize data, you’ll often deal with JSON strings after converting values like this.

Method 2 – toString()

Call toString() on the number itself.   Simple Ways to Convert Strings and Numbers in JavaScript  28   Steps: Create a number. Call .toString() on it. Print the result.   Simple Ways to Convert Strings and Numbers in JavaScript  31     Simple Ways to Convert Strings and Numbers in JavaScript  30     Simple Ways to Convert Strings and Numbers in JavaScript  29  
const num = 123;
const str = num.toString();

console.log(str);        // "123"
console.log(typeof str); // "string"
  Simple Ways to Convert Strings and Numbers in JavaScript  32  

Final thoughts

To convert a string into a number, use Number() for general cases, parseInt() when you only need the integer part, parseFloat() when you want the decimal part, and the unary + for a quick inline conversion. To convert a number into a string, use String() or toString() based on what reads better in your code. Practice these patterns once, and they’ll become second nature.

Leave a Comment