parseInt(input, radix)
parseInt()
method parses a string to an integer based on a specified radix number in 2nd argument.- It only parses the very first string, any white spaces in between will be considered the end of the parsing.
- The return type is an
Integer
where the decimals are ignored. - The second argument (optional) is based on the radix number system
Radix parameter
A radix parameter specifies the number system to use mainly of four systems
- Binary system specified as
2
- Octal number system specified as
8
- Decimal number system specified as
10
- Hexadecimal number system specified as
16
Default radix
_18const number = "5";_18console.log(number + 5); // "55"_18parseInt(number + 5); // 55_18// parsing defaults to Decimal(10)_18_18const number = "5.5";_18console.log(number + 5); // "5.55"_18parseInt(number + 5); // 10_18// The decimal will be ignored_18_18const input = "53abc";_18console.log(number + 5); // "53abc5"_18parseInt(number) + 5; // 58_18// The value "abc" is not a number and js will ignore them_18_18// Tricky one ๐_18parseInt("-0XF"); // -15_18// parsing defaults to hexadecimal since begins with `0x`
Specified radix
_16const input = "5 3abc";_16console.log(number + 5); // "5 3abc5"_16parseInt(number + 5); // 10_16// Because the white space after 5 will be considered the end_16_16parseInt("100", 2); // 4_16// Takes binary number system._16_16parseInt("100", 8); // 64_16// Considers octal number system._16_16parseInt("100", 10); // 100_16// Which is the decimal number system and the default radix parameter as well_16_16parseInt("100", 16); // 256_16// 16 belongs to the hexadecimal number system.
@ragavkumarv
swipe to next โก๏ธ