Examples: Convert String to int in JavaScript


In JavaScript, you can represent an integer both as a String or a number as shown below,

var myIntNumber = 100; //Type Number
var myIntString = '100'; //Number as type String

Now if you compare these two variables with == (double equals) comparison you will the result as true,

var myIntNumber = 100;
var myIntString = '100';

if(myIntNumber == myIntString) { //true
 console.log("myIntNumber and myIntNumber are equal"); 
}
Output:

myIntNumber and myIntNumber are equal

But what if you do a strict comparison with === (triple equals) you will get false,

var myIntNumber = 100;
var myIntString = '100';

if(myIntNumber === myIntString) { //false
 console.log("myIntNumber and myIntNumber are equal"); 
} else {
 console.log("myIntNumber and myIntNumber are not equal"); 
}
Output:

myIntNumber and myIntNumber are not equal

✌️So as you could conclude "JavaScript is a loosely typed language", as you can see I do not need to specify the type as int yet == comparison gives me a true result. But it simply backfired when I used ===



Converting String to int in JavaScript

You can make use of the parseInt() function to convert a string to integer in JavaScript.

Syntax:

parseInt(string)

The function takes in one argument, that is a string value, and returns an integer value of the string.

Examples:

1- var myIntStr = '100';
2- 
3- console.log(parseInt(myIntStr));
4-
5- console.log(parseInt(myIntStr) == 100);
6- console.log(parseInt(myIntStr) === 100);
7- 
8- console.log(parseInt(myIntStr) == '100');
9- console.log(parseInt(myIntStr) === '100');
Output

100
true
true
true
false

As you can see in the above examples: At line number 3, the parseInt(myIntStr) returns a value 100 of type integer. Now when we compare the int value with both double and triple equal compression we get true as a result. On lines 8 and 9 we compare it with String value 100 and we get true and false results as expected.



Question) What is the number passed to parseInt() is a String?

You may have a question: What is the return value if I pass a non-numerical number as an input to javaScript parseInt function, lets try doing it,

Example:
var myInvalidIntStr = 'Hello-JavaScript';

console.log(parseInt(myInvalidIntStr));
Output:

NaN

As you can see when I try to convert a non-numeric value using parseInt I get NaN (Not a Number) as an output.



Question) What if we try to pass an argument to parseInt that is a decimal number?

Well that's again a good question, let's try it out!

Example: |CBS|htmlvar myDecimalNumer1 = '10.15'; var myDecimalNumer2 = '10.95'; console.log(parseInt(myDecimalNumer1)); console.log(parseInt(myDecimalNumer2)); Output:

10
10

As expected, the function returns an integer value of the decimal. A point to note here is there is no rounding off (round up or round down) that happens here it just returns the integer value and decimals are ignored.



Question) What if the String has Number formatting using a comma?

Another good question❗️Number with comma's could have multiple meaning from the region to region like in Europe comma is used instead of decimals point (Locale). Well explained here - https://stackoverflow.com/questions/11665884/how-can-i-parse-a-string-with-a-comma-thousand-separator-to-a-number

var myNumStrWithComma1 = '10,15';
var myNumStrWithComma2 = '1,000,000';
console.log(parseInt(myNumStrWithComma1));
console.log(parseInt(myNumStrWithComma2));
Output:

10
1

As you can see the comma is treated as a decimal by default. The number with multiple commas just returns the 1st value before the comma.



Question) Convert String to int without using parseInt method?

There are other ways of converting String to integers one of them using Number() function, lets try all that we have learned till now using the Number function.

Example:
var myIntStr1 = '100';
var myInt2 = 100;
var myInvalidStrNum = 'hello';
var myIntStrWithComma1 = '1,00,000';
var myIntStrWithComma2 = '1,95';
var myDecimalStr1 = '10.15';
var myDecimalStr2 = '10.95';

console.log(Number(myIntStr1)); //100
console.log(Number(myIntStr1) === 100); //true
console.log(Number(myIntStr1) === '100'); //false
console.log(Number(myInvalidStrNum)); //NaN
console.log(Number(myIntStrWithComma1)); //NaN
console.log(Number(myIntStrWithComma2)); //NaN
console.log(Number(myDecimalStr1)); //10.15
console.log(Number(myDecimalStr2)); //10.95
JavaScript String to int conversion Examples
JavaScript String to int conversion Examples


Have Questions? Post them here!


















Copyright Β© Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap