Javascript Arithmetic Operators
Javascript Arithmetic Operators
and more.
Arithmetic Operators
Comparison Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators
String Operators
Type Operators
Bitwise Operators
JavaScript Arithmetic Operators –
Arithmetic operators perform arithmetic operations on numbers.
Operator Description
+ Addition
– Subtraction
* Multiplication
/ Division
% Modulus (Remainder)
++ Increment
— Decrement
Example –
1 // Addition
2 var a = 4;
3 var b = 3;
4 var x = a + b; // adding 2 variables
5 var x = (100 + 50) * a; // expressions
6
7 // Subtraction
8 var x = 5;
9 var y = 2;
10var z = x - y;
11
12// Multiplication
13var x = 5;
14var y = 2;
15var z = x * y;
16
17// Division
18var x = 5;
19var y = 2;
20var z = x / y;
21
22// Modulo
23var x = 5;
24var y = 2;
25var z = x % y;
26
27// Increment
28var x = 5;
29x++;
30var z = x;
31
32// Decrement
33var x = 5;
34x--;
35var z = x;
Associativity –
Associativity determines the way in which operators of the same precedence are parsed. For example, consider an
expression:
1a OP b OP c // OP means a operator
Left-associativity (left-to-right) means that it is processed as (a OP b) OP c, while right-associativity (right-to-left)
means it is interpreted as a OP (b OP c).
Operator Precedence –
Operator precedence describes the order in which operations are performed in an arithmetic expression.
Example –
1var x = 100 + 50 * 3;
As in traditional school mathematics, the multiplication is done first. Multiplication (*) and division (/) have
higher precedence than addition (+) and subtraction (-).
Postfix Decrement … --
typeof typeof …
void void …
delete delete …
await await …
Division … / …
Remainder … % …
Subtraction … - …
in … in …
instanceof … instanceof …
Inequality … != …
… += …
… -= …
… **= …
… *= …
… /= …
… %= …
… <<= …
… >>= …
… >>>= …
… &= …
… ^= …
… |= …
yield* yield* …
Given that x = 6 and y = 3, the table below explains the logical operators:
Example –
Syntax –
1John Doe
The += assignment operator can also be used to add (concatenate) strings:
Example –
1var x = 5 + 5;
2var y = "5" + 5;
3var z = "Hello" + 5;
Output –
110
255
3Hello5