Blog

Numbers and Operations on Numbers in JavaScript

A Number type A numeric value in JavaScript is a double-precision, 64-bit binary format, IEEE 754 value. The storage format is:
0-51: fraction, 52-62: exponent, bit 63 is sign.  The highest value that can be represented internally is 253 which is 9,007,199,254,740,994. Then they are stored as powers of 2(2exponent).  Special values for numbers are:

  • Nan. Not a number. Any expression with NaN is also NaN.
  • Infinity. A number greater than 1.7976931348623157E + 10308.
  • -Infinity. A number below than -1.7976931348623157E + 10308.
  • undefined. No value has been assigned.
  • Arithmetic Operators JavaScript supports the following arithmetic operators. They are:
  • / : division
  • * : multiplication
  • % : modulo. This gives the remainder.
  • + : addition
  • – : substraction

Operator Precedence JavaScript has a 2 level operator precedence.
/,*,% at one level
+,- at the second level.

Java also supports two unary number operators + and -.
Thus +5, -2 etc are allowed.

Expression: var x1 = 1 + 2, x2 = 1 – 2, x3 = 2 * 3, x4 = 3 % 4, x5 = 3 / 2;
Result: x1=3, x2=-1, x3=6, x4=3, x5=1.5

Expression:
var x1 = 1 + 2 * 3; =1+(2*3)=7
var x2 = 1 * 2 + 3; =(1*2) + 3=5
var x3 = 1 / 2 / 2 / 2; =(((1/2)/2)/2)=((.5/2)/2)=(.25/2)=.125
var x4 = 1 / 2 / 2 * 1; =(((1/2)/2)*1)=((.5/2)*1)=.25*1=.25
var x5 = 1 / 2 / 1 * 2;=(((1/2)/1)*2)=((0.5)/1)*2)=(.5*2)=1

Result: x1=7, x2=5, x3=0.125, x4=0.25, x5=1

Expression:
var a=1; a =1
var b=++a; b=2, a=2
var c=1; c=1
var d=c++; d=1, c=2
var e=1,f=3; e=1, f=3
var g=++e – –e * f++ – ++f; 2 – 1*3 -5.  and e=1 , f=5

Result: a=2, b=2, c=2, d=1, e=1, f=5, g=-6

Expression:
var a=0,b=1,c=2;
var d=(a+(b+c)/c + a)*(b+a);

Result: a=0, b=1, c=2, d=1.5

Some examples at this link.
Arithmetic Operators

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *