+ 4

When We declare a variable why do we get different value if we add 0's in the beginning?

say a var x=001400 document.write(x) The output is 768 ....why

9th Dec 2016, 3:10 PM
Anindya
Anindya - avatar
2 Answers
+ 3
It happens because..it is converted from hexadecimal format to the normal format. because javascript think it is a hexadecimal number that u declared..since it has preceding zeros.
9th Dec 2016, 3:37 PM
Rithu.O.R
0
when ever u add 0x.... before to number it consider as hexadecimal number if u add 0.... it consider as octal(base 8) number If you do alert or doc..write, by default they convert them to string default convention of number toString is with base 10, to get exact value as what u give, write x.toString(8) but in string conversion obviously it removes preceding zeros of number
14th Feb 2017, 10:53 AM
dathatreya