# How to parse number from string in JavaScript?

In this article, we will learn **how to parse number from string in JavaScript**? JavaScript has method for it, known as **'parseInt()' and 'parseFloat()' respectively to parse integer and float values from the string**.

Submitted by Abhishek Pathak, on October 27, 2017

In JavaScript, most the data is handled in the form of Strings. Taking input from user and appending the output in DOM, is all done in String data type. Let's suppose we take input from user and we require a number input, but the input will be in form of string, hence we need to parse a number from this string. This article will teach you how to do this in JavaScript.

Let us assume that we take input from prompt() function. We can also use input fields, but using prompt function it is easy to show the core functioning of the string parsing.

var input = prompt('Enter a number');

*Now*, we get a string with a number in it. Using the Number() constructor function which is also used to create new numbers, we can do a explicit type conversion here to **convert this string into Number**.

**Code**

var input = prompt('Enter a number'); var number = Number(input);

*Now*, we get a number with us that were actually a string when user input it. But note that this works only when the entire string contains nothing other than digits. If it contains, suppose any character, it will return NaN value. The NaN value stands for not a number which can't be used for mathematical operations.

*But*, JavaScript has another method for it, known as parseInt() and parseFloat() respectively to parse integer and float values from the string. The numbers should be present at the beginning of the string and before any other character. These functions return numbers parsed till either the string ends or encounters first character. Let's see parseInt and parseFloat in action.

We have the same, prompt code, that will store the input string, var input = prompt('Enter a number')

*Now*, we will use different functions to show the returned values from these functions.

**Code**

// Input: 30abc console.log(parseInt(input)); // Output: 30 // Input: 30a500 console.log(parseInt(input)); // Output: 30 // Input: 30.05abc console.log(parseInt(input)); // Output: 30 // Input: 30.05abc console.log(parseFloat(input)); // Output: 30.05

This is **how we can parse numbers, integers and floating values from the strings**? Hope you like the article. Please share your thoughts and comments.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.