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

Have Questions? Post them here!
- How to get UTC (GMT) time using JavaScript
- Submit html form on dropdown menu value selection or change using javascript
- How to detect Browser and Operating System Name and Version using JavaScript
- JavaScript : Get current page address
- [javaScript] Convert text case to lowercase
- How to check if a String contains substring or a word using javaScript
- Writing your first Hello, World! 🌍 JavaScript code Tutorial
- JavaScript: Check if variable is a number
- How to send email from JavaScript HTML using mailto
- Meaning of javascript:void(0) explained with example
- How to get query string in JavaScript HTML location.search
- Javascript convert text case from uppercase to lowercase
- Loading previous page using html button using JavaScript
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP
- How to get current URL Location using Javascript HTML
- Detect if Cookies are enabled using JavaScript
- Write javaScript code in Swedish using FikaScript
- Add Animated Scrolling to Html Page Title Script
- JavaScript: Convert an Image into Base64 String
- Remove items from JavaScript array
- Send Extra Data with Ajax Get or Post Request
- How to Print from JavaScript HTML using window.print
- Get Device Screen Width and Height using javaScript
- Examples: Convert String to int in JavaScript
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
- How to initiate a photo request on iPhone from Mac Monterey - iOS
- Calculate discount amount python code - Python
- Failed to find provider info for com.facebook.katana.provider.PlatformProvider - Android
- Remove Trailing zeros BigDecimal Java - Java
- How to change directory in Git bash - Git
- [Solved] SharePoint Access Denied error editing Document Name - SharePoint
- Share Multiple Images in WhatsApp using Android Intent - WhatsApp
- Permanently Set or Change $JAVA_HOME on Mac (macOS) - MacOS