+ 4
Different output , why?
var m = parseInt("0*10"); var k = parseInt("0x10"); console.log(m); console.log(k);
11 Réponses
+ 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!
+ 5
Md. Iftakher Hossain
0x means the number is hex, or base 16.
0x10 is 16
+ 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
+ 4
Thanks for the correction bro :)) Kelvin Paul
+ 3
Arb Rahim Badsa
You are welcome 😌😉
+ 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
+ 2
Thanks all.
+ 2
Kelvin Paul Thanks.
+ 1
("0*10") and ("0x19")
you are trying to parse it to Integer.
+ 1
Arb Rahim Badsa Thanks.
0
Md. Iftakher Hossain you are welcome ☺