4

I know javascript. but I want to know why is it giving 83 answers? how does it work? The alert() method displays an alert box with a specified message and an OK button.

An alert box is often used if you want to make sure information comes through to the user.

But I am unable to find logic for this answer? thanks for the help

Sh Huzaifa
  • 127
  • 7

2 Answers2

5

Because it is an Octal Number system if you start with 0.

and if starts with 0x it is hexadecimal System.

If you are looking to convert the value to number, then parseInt method, accepts the second parameter as radix (the base in mathematical numeral systems)

parseInt( "0123", 8 );  => 83
parseInt( "0123", 10 );  => 123
parseInt( "0123", 16 );  => 291

More from MDN

Integers can be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8) and binary (base 2).

Decimal integer literal consists of a sequence of digits without a leading 0 (zero).

Leading 0 (zero) on an integer literal, or leading 0o (or 0O) indicates it is in octal. Octal integers can include only the digits 0-7.

Leading 0x (or 0X) indicates hexadecimal. Hexadecimal integers can include digits (0-9) and the letters a-f and A-F.

Leading 0b (or 0B) indicates binary. Binary integers can include digits only 0 and 1.

Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41
1

If you put a 0 in front of a number, JavaScript will treat it as an octal number, which is a base 8 number.

If you would like to display a regular number, it is a good JavaScript practice to omit the leading zero.

Visit this website to learn more about the Octal System

https://en.wikipedia.org/wiki/Octal

Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87
  • Tell me if I am correct. does it convert octal number to decimal number to display the alert message? – Sh Huzaifa Jan 27 '17 at 18:02
  • I'm not sure. But 0 in-front of a number means octal – Richard Hamilton Jan 27 '17 at 18:07
  • @ShHuzaifa: There's no octal vs decimal number in JS. They're all just numbers, and when you get down to it, they're binary, so there's no numeric conversion that takes place. You're conflating the syntax for creating data, with the data itself. You can get alternate display strings by using `.toString(8)`, where `8` is whatever base you want to use for the display. The default is `10`, which explains why an `alert()` shows that format. –  Jan 27 '17 at 18:13
  • that make sense. Default base is 10 that's why it shows number into base 10. – Sh Huzaifa Jan 27 '17 at 18:21