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 Run JavaScript on Mac Terminal
- Get Current time in GMT/UTC using JavaScript
- How to yarn reinstall all Packages
- [javaScript] Convert text case to lowercase
- Get Device Screen Width and Height using javaScript
- Fix - npm start: sh: index.js: command not found
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP
- JavaScript: Convert an Image into Base64 String
- JavaScript : Get current page address
- How to get query string in JavaScript HTML location.search
- Create React App using npm command with TypeScript
- JavaScript: Count Words in a String
- Add Animated Scrolling to Html Page Title Script
- How to send email from JavaScript HTML using mailto
- Javascript convert text case from uppercase to lowercase
- Submit html form on dropdown menu value selection or change using javascript
- Send Extra Data with Ajax Get or Post Request
- Fix: SyntaxError: The requested module does not provide an export named default
- Examples: Convert String to int in JavaScript
- 10 ways to Convert String to a Number in JavaScript
- Excel Fix: SECURITY RISK Microsoft has blocked macros from running because the source of this file is untrusted.
- Fix: ReferenceError: require is not defined in ES module scope [Node]
- [JavaScript] Remove all Newlines From String
- How to detect Browser and Operating System Name and Version using JavaScript
- Docker - Incompatible CPU detected - M1/M2 Mac (macOS Sonoma) - Docker
- How to install npm on Ubuntu - Linux
- Command to check Last Login or Reboot History of Users and TTYs - Linux
- How to Copy all text to Clipboard in Vim - vi
- Program 38: Store two numbers in variables and print their sum - Python-Programs
- Fix - Microsoft Teams Operation Failed With Unexpected Error - Teams
- Bash Command to Find OS Version in Terminal - Bash
- How to check PowerShell version - Powershell