How To Convert A String To An Integer In Javascript

Convert a string to an integer in javascript

To convert a string to an integer in javascript you need to know about data types and some methods that work with convert. In addition, you must know about the methods used to convert or how these methods work. See the instructions below for all answers.

Convert a string to an integer in javascript

Method 1: Using parseInt() method

To convert a string to an integer in javascript is relatively easy. The most common way is probably the parserInt() method. When using this method, all data types are returned as number integers. The value depends on the value, and the input data type can be determined.

Syntax:

parseInt(value)

Parameter:

  • value: The value to convert the type number integer.

Follow the example below to understand better:

var str = '123';
var numInt = parseInt(str);

// Print value and datatype
console.log('Data type of numInt is: ' + typeof(numInt));
console.log('Value of numInt is: ' + numInt);

Output:

Data type of numInt is: number
Value of numInt is: 123

So, based on the above message, we can determine the data type of numInt is a number, and the value is 123.

Note: if the input value is not a number but another data type, then using the parseInt() method to convert it will still return a data type of number, but the value is NaN.

For example:

var myName = 'Edward';
var myNameConvert = parseInt(myName);

// Print value and datatype
console.log('Data type of myNameConvert is: ' + typeof(myNameConvert));
console.log('Value of myNameConvert is: ' + myNameConvert);

When it returns a value of NaN, we cannot use it to calculate like a normal Number type, so when using this method, please pay attention to your input data.

Method 2: Using Number() object

Number() is a predefined object in javascript. We can use it to cast from string to number easily.

Syntax:

Number(value)

Parameter: 

  • value: The value to convert the type number integer.

Below I will give an example, how to use it and some points to note when using this method.

Example:

var str = '123';
var numInt = Number(str);

// Print value and datatype
console.log('Data type of numInt is: ' + typeof(numInt));
console.log('Value of numInt is: ' + numInt);

Output:

Data type of numInt is: number
Value of numInt is: 123

However, with the input value being a string with a value of type float, the output is a number type with a value of float as follows:

var str = '111.111';
var numFloat = Number(str);

// Print value and datatype
console.log('Data type of numFloat is: ' + typeof(numFloat));
console.log('Value of numFloat is: ' + numFloat);

Output:

Data type of numFloat is: number
Value of numFloat is: 111.111

With the above result, consider using this method with reasonable input.

Summary

Both ways, parseInt() and Number() can convert a string to an integer in javascript. However, each method has its own strengths and weaknesses. Depending on your input value, it will give true or false. Apply an intelligent way to solve your project.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *