The parseInt() function in JavaScript is used to parse a string and return an integer. It takes a string as an argument and tries to convert it into an integer based on the specified radix (base).
Syntax
parseInt(string, radix)
· string: The string to be parsed. It can also be a variable containing a string. If it encounters a non-numeric character (except for leading whitespace) while parsing the string, it stops parsing and ignores the rest of the string.
· radix (optional): An integer between 2 and 36 that represents the base of the numeral system to be used. If the radix is not provided, parseInt() may interpret the number in decimal (base 10), hexadecimal (base 16 if the string starts with 0x or 0X), or other bases depending on the string content.
1. Basic Decimal Parsing
console.log(parseInt("123")); // Output: 123
console.log(parseInt("123.45")); // Output: 123 (ignores the fractional part)
2. Parsing with Radix
console.log(parseInt("1010", 2)); // Output: 10 (binary to decimal)
console.log(parseInt("FF", 16)); // Output: 255 (hexadecimal to decimal)
console.log(parseInt("77", 8)); // Output: 63 (octal to decimal)
3. Handling Non-Numeric Characters
console.log(parseInt("123abc")); // Output: 123 (stops parsing at 'a')
console.log(parseInt("abc123")); // Output: NaN (no valid number at the start)
4. Leading Whitespace
console.log(parseInt(" 456")); // Output: 456 (leading whitespace is ignored)
5. Default Radix
console.log(parseInt("10")); // Output: 10 (decimal by default)
console.log(parseInt("0x10")); // Output: 16 (interprets as hexadecimal)
6. Using Radix 10 Explicitly
console.log(parseInt("10", 10)); // Output: 10 (explicitly using base 10)
console.log(parseInt("010", 10)); // Output: 10 (leading zero is ignored in base 10)
7. Invalid Radix or String
console.log(parseInt("123", 37)); // Output: NaN (radix must be between 2 and 36)
console.log(parseInt("123", 1)); // Output: NaN (radix must be between 2 and 36)
console.log(parseInt("Hello", 8)); // Output: NaN (no digits in base 8)
8. Parsing Negative Numbers
console.log(parseInt("-123")); // Output: -123
console.log(parseInt("-0xF", 16)); // Output: -15 (negative hexadecimal)
9. Parsing with Leading Zeros
console.log(parseInt("010")); // Output: 10 (leading zero is ignored)
console.log(parseInt("010", 8)); // Output: 8 (octal interpretation)
console.log(parseInt("010", 10)); // Output: 10 (decimal interpretation)
parseIntDemo.js
console.log(`parseInt("123") : ${parseInt("123")}`);
console.log(`parseInt("123.45") : ${parseInt("123.45")}`);
console.log(`parseInt("1010", 2) : ${parseInt("1010", 2)}`);
console.log(`parseInt("FF", 16) : ${parseInt("FF", 16)}`);
console.log(`parseInt("77", 8) : ${parseInt("77", 8)}`);
console.log(`parseInt("123abc") : ${parseInt("123abc")}`);
console.log(`parseInt("abc123") : ${parseInt("abc123")}`);
console.log(`parseInt(" 456") : ${parseInt(" 456")}`);
console.log(`parseInt("10") : ${parseInt("10")}`);
console.log(`parseInt("0x10") : ${parseInt("0x10")}`);
console.log(`parseInt("10", 10) : ${parseInt("10", 10)}`);
console.log(`parseInt("010", 10) : ${parseInt("010", 10)}`);
console.log(`parseInt("123", 37) : ${parseInt("123", 37)}`);
console.log(`parseInt("123", 1) : ${parseInt("123", 1)}`);
console.log(`parseInt("Hello", 8) : ${parseInt("Hello", 8)}`);
console.log(`parseInt("-123") : ${parseInt("-123")}`);
console.log(`parseInt("-0xF", 16) : ${parseInt("-0xF", 16)}`);
console.log(`parseInt("010") : ${parseInt("010")}`);
console.log(`parseInt("010", 8): ${parseInt("010", 8)}`);
console.log(`parseInt("010", 10) : ${parseInt("010", 10)}`);
Output
parseInt("123") : 123 parseInt("123.45") : 123 parseInt("1010", 2) : 10 parseInt("FF", 16) : 255 parseInt("77", 8) : 63 parseInt("123abc") : 123 parseInt("abc123") : NaN parseInt(" 456") : 456 parseInt("10") : 10 parseInt("0x10") : 16 parseInt("10", 10) : 10 parseInt("010", 10) : 10 parseInt("123", 37) : NaN parseInt("123", 1) : NaN parseInt("Hello", 8) : NaN parseInt("-123") : -123 parseInt("-0xF", 16) : -15 parseInt("010") : 10 parseInt("010", 8): 8 parseInt("010", 10) : 10
No comments:
Post a Comment