Hello everyone, hope you are all doing well. My name is Surya L, and the aim of this blog is to teach you all about parseInt Function & parseInt Function with Redix in JavaScript.
The parseInt() function parses a string and returns an integer. Here's an example:
const a = parseInt("076");
The above function converts the string 076 to the integer 76. If the first character in the string can't be converted into a number, then it returns NaN.
function convertToInteger(str) {
return parseInt(str);
}
console.log(convertToInteger("56"))
Converts the string value in the variable str to integer.
The Output of above code is 👇
56
The parseInt() function parses a string and returns an integer. It takes a second argument for the radix, which specifies the base of the number in the string. The radix can be an integer between 2 and 36.
The function call looks like:
parseInt(string, radix);
const a = parseInt("11", 2);
According to the radix variable, 11 is in the binary system, or base 2. The string 11 is converted to an integer 3 in this example.
function convertToInteger(str) {
return parseInt(str,2);
}
console.log(convertToInteger("101"))
The string 101 is equal to 5 in Binary System
function convertToInteger(str) {
return parseInt(str,8);
}
console.log(convertToInteger("101"))
The string 101 is equal to 65 in Binary System
function convertToInteger(str) {
return parseInt(str,16);
}
console.log(convertToInteger("101"))
The string 101 is equal to 257 in Binary System
- The parseInt() function parses a string and returns an integer.
- It takes a second argument for the radix, which specifies the base of the number in the string. The radix can be an integer between 2 and 36.
Thanks for reading the blog. Do let me know what you think about it.
You can connect me with Showwcase Twitter Blog GitHub Contact Me
Refernce: I learned this topics in FreeCodeCamp which I explained in minified version