WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Arithmetic



JavaScript Arithmetic Operators

Arithmetic administrators perform number-crunching on numbers (literals or variables).

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
- - Decrement

Arithmetic Operations

A ordinary number-crunching activity works on two numbers.

The two numbers can be literals:

Example

var x = 70 + 80;

or variables:

Example

var x = a + b;

or expressions:

Example

var x = (70 + 80) * a;

Operators and Operands

The numbers (in a number-crunching task) are called operands.

The activity (to be performed between the two operands) is characterized by a operator.

Operand Operator Operand
70 + 80

The addition administrator (+) includes numbers:

Adding

var x = 4;
var y = 3;
var z = x + y;

The subtraction administrator (- ) subtracts numbers.

Subtracting

var x = 4;
var y = 3;
var z = x - y;

The multiplication administrator (*) increases numbers.

Multiplying

var x = 4;
var y = 3;
var z = x * y;

The division administrator (/) separates numbers.

Dividing

var x = 4;
var y = 3;
var z = x/y;

The modular administrator (%) restores the division remainder.

Modulus

var x = 4;
var y = 3;
var z = x % y;

The increment administrator (++) increases numbers.

Incrementing

var x = 4;
x++;
var z = x;

The decrement administrator (- - ) decrements numbers.

Decrementing

var x = 4;
x- - ;
var z = x;

Operator Precedence

Operator priority depicts the request in which activities are performed in a number juggling expression.

Example

var x = 70 + 80 * 3;

Is the consequence of model over equivalent to 180 * 3, or is it equivalent to 70 + 180?

Is the expansion or the duplication done first?

As in customary school arithmetic, the augmentation is done first.

Multiplication (*) and division (/) have higher precedence than expansion (+) and subtraction (- ).

And (as in school science) the priority can be changed by utilizing parentheses:

Example

var x = (70 + 80) * 3;

When utilizing enclosures, the tasks inside the brackets are registered first.

When numerous tasks have a similar priority (like expansion and subtraction), they are figured from left to right:

Example

var x = 70 + 80 - 3;

JavaScript Operator Precedence Values

Value Operator Description Example
19 ( ) Expression grouping (3 + 4)
       
18 . Member person.name
18 [] Member person["name"]
       
17 () Function call myFunction()
17 new Create new Date()
       
16 ++ Postfix Increment i++
16 - - Postfix Decrement i- -
       
14 ++ Prefix Increment ++i
14 - - Prefix Decrement - - i
14 ! Logical not !(x==y)
14 typeof Type typeof x
       
14 * Multiplication 10 * 4
14 / Division 10/4
14 % Modulo division 10 % 4
14 ** Exponentiation 10 ** 3
       
13 + Addition 10 + 4
13 - Subtraction 10 - 4
       
13 << Shift left x << 3
13 >> Shift right x >> 3
       
11 < Less than x < y 
11 <= Less than or equal x <= y
11 > Greater than x > y
11 >= Greater than or equal x >= y
       
10 == Equal x == y
10 === Strict equal x === y
10 != Unequal x != y
10 !== Strict unequal x !== y
       
6 && And x && y
4 || Or x || y
       
3 = Assignment x = y
3 += Assignment x += y
3 -= Assignment x - = y
3 *= Assignment x *= y
3 /= Assignment x/= y