Understanding  Numeric Literals in JavaScript

Understanding Numeric Literals in JavaScript

understanding numeric literals in JavaScript

  • JavaScript treats all numbers the same, so you don't have to do anything when working with the two basic numbers.

Integers-

  • These are the numbers that do not have a fraction or decimal part. So you represent an integer using a sequence of one or more digits.

  • Example- 0

42

168

2005

Floating-point numbers-

  • These are the numbers that do have a fractional or decimal part. So we represent the floating-point numbers by first writing the integer part and then writing the decimal part and at last writing the fractional part.

Example- 0.012

5.1234

-185.325974

7.1245e+32

45.321423E-654

Exponential Notation-

  • In the above examples, the last two examples are a bit different from the others. They need some more explanation.

  • Exponential notation uses an (e or E) followed by a plus sign (+) or a minus sign (-) which is called an exponent.

  • If the notation contains a plus sign, then you have to multiply the first part of the number (the part before e or E) by 10 to the power of the exponent.

  • Example- 9.87654e+5

    Here the exponent is 5, and 10 to the power 5 is 100,000

    So multiplying 9.87654 by 100,000 we get 987,654

  • Example- 3.4567e-4

    Here the exponent is 4, and 10 to the power 4 is 10,000

  • So dividing 3.4567 by 10,000 we get 0.00034567

Hexadecimal values-

  • The hexadecimal number system uses the digits from (0-9) and the letter from (A to F)

  • These letters represent the numbers from (10 to 15) So what is in the decimal number would be 16 is actually 10 in the hexadecimal number.

  • To describe the hexadecimal number in JavaScript, the number begins with (0x or x0)

  • Example- 0x56

x045

0xff

Conclusion-

  • Understanding numeric literals (integers, floating-point numbers, exponential notations, hexadecimal integer values)