+ 4

Different output , why?

var m = parseInt("0*10"); var k = parseInt("0x10"); console.log(m); console.log(k);

4th May 2020, 2:43 PM
Md Iftakher Hossain
Md Iftakher Hossain - avatar
11 odpowiedzi
+ 9
Md. Iftakher Hossain Hello there :)) 0x is base 16 that is, anything after it will be considered as a hexadecimal value. To check : console.log(0x1) // 1 console.log(0x9) // 9 console.log(0x10) // 16 Because 10 in hexadecimal means 16 in decimal. Proof : console.log(0x10 == 16) // true So parsing it as a int will result in 16. And 0*10 = 0, so parsing it as a int will result in 0!
4th May 2020, 2:55 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 5
Md. Iftakher Hossain 0x means the number is hex, or base 16. 0x10 is 16
4th May 2020, 2:49 PM
BroFar
BroFar - avatar
+ 4
Arb Rahim Badsa Slight correction: parseInt("0*10") // output is 0, extracting the characters from the beginning of the string if it is a number character. And convert it into number. It's not an evaluation like eval() method. you already know😉 Eg: parseInt("3*5") //output : 3
4th May 2020, 3:07 PM
Kelvin Paul
Kelvin Paul - avatar
+ 4
Thanks for the correction bro :)) Kelvin Paul
4th May 2020, 3:10 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 3
Arb Rahim Badsa You are welcome 😌😉
4th May 2020, 3:13 PM
Kelvin Paul
Kelvin Paul - avatar
+ 3
Md. Iftakher Hossain parseInt ("10",8) // returns 8 It converts base8 number 10 into base10: Octal 10 is equal to Decimal 8 You can use parseInt method to convert any base to decimal value. (From base2 to base32) toString() is much similar to parseInt. 0x45.toString(8) // returns 105 Converting hex to octal I mean var x = 0x45 // hex number console.log(x.toString(8)) // Output:105
4th May 2020, 5:08 PM
Kelvin Paul
Kelvin Paul - avatar
+ 2
Thanks all.
4th May 2020, 2:54 PM
Md Iftakher Hossain
Md Iftakher Hossain - avatar
+ 2
Kelvin Paul Thanks.
5th May 2020, 2:42 AM
Md Iftakher Hossain
Md Iftakher Hossain - avatar
+ 1
("0*10") and ("0x19") you are trying to parse it to Integer.
4th May 2020, 2:49 PM
Dasarath Singh
+ 1
4th May 2020, 3:05 PM
Md Iftakher Hossain
Md Iftakher Hossain - avatar
0
Md. Iftakher Hossain you are welcome ☺
5th May 2020, 3:12 AM
Kelvin Paul
Kelvin Paul - avatar