Javascript Operators - 11 types (Reference)
The javascript operators are used to perform operations on variables like mathematical operations, logical operations, and comparison operations.
Operators:
Arithmetic
The Arithmetic operators are used to perform mathematical operations on variables.
// Addition +
x = 5 + 2; // x = 7
// Subtraction -
x = 5 - 2; // x = 3
// Multiplication *
x = 5 * 2; // x = 10
// Division /
x = 5 / 2; // x = 2.5
// Modulus (division remainder) %
x = 5 % 2; // x = 1
// Increment ++
x = ++6; // x = 6
x = 6++; //x = 5
// Decrement --
x = --4; // x = 4
x = 4--; // x = 5
// Exponentiation **
x = 5 ** 2; // x = 25
Assignment
The assignment operator is used to assign a value to a variable.
var x = 10;
// Equal to =
x = 5; // x = 5
// Add to variable +=
x += 5; // x = 15
// Subtract to variable -=
x -= 5; // x = 5
// Multiply to variable *=
x *= 5; // x = 50
// Divide to variable /=
x /= 5; // x = 2
// Modulus to variable %=
x %= 5; // x = 0
// Left shift to variable <<=
// Right shift to variable >>=
// Right shift unsigned to variable >>>=
// AND to variable &=
// XOR to variable ^=
// OR to variable |=
Comparison
The comparison operators are used to compare two values.
// Equal to ==
x == 8; // false
// Equal value and equal type ===
x === "5"; // false
// Not equal !=
x != 8; // true
// Not equal value or not equal type !==
x !== "5"; // true
// Greater than >
x > 8; // false
// Less than <
x < 8; // true
// Greater than or equal to >=
x >= 8; // false
// Less than or equal to <=
x <= 8; // true
Conditional / Ternary
The conditional operator is used to evaluate a condition and return one of two values.
var x = (condition) ? if : else;
Logical
The logical operators are used to evaluate multiple conditions.
// and &&
(6 < 10 && 3 > 1) is true
// or ||
(6 === 5 || 3 === 5) is false
// not !
!(6 === 3) is true
Bitwise
the bitwise operators are used to perform bitwise operations on variables.
// AND &
x = 5 & 1;
0101 & 0001; // 0001 // 1
// OR |
x = 5 | 1;
0101 | 0001; // 0101 // 5
// NOT ~
x = ~5;
~0101; // 1010 // 10
// XOR ^
x = 5 ^ 1;
0101 ^ 0001; // 0100 // 4
// Left shift <<
x = 5 << 1;
0101 << 1; // 1010 // 10
// Right shift >>
x = 5 >> 1;
0101 >> 1; // 0010 // 2
// Right shift unsigned >>>
x = 5 >>> 1;
delete
the delete operator is used to delete a property from an object.
var obj = {
111 : "aaa",
222 : "bbb",
333 : "ccc"
};
delete obj.333;
in
The in operator is used to check if a property exists in an object. The in operator returns true if the specified property is in the specified object, otherwise false.
// Arrays
var cars = ["Saab", "Volvo", "BMW"];
"Saab" in cars; // Returns false (specify the index number instead of value)
0 in cars; // Returns true
1 in cars; // Returns true
4 in cars; // Returns false (does not exist)
"length" in cars; // Returns true (length is an Array property)
// Objects
var person = { firstName: "John", lastName: "Doe", age: 50 };
"firstName" in person; // Returns true
"age" in person; // Returns true
// Predefined objects
"PI" in Math; // Returns true
"NaN" in Number; // Returns true
"length" in String; // Returns true
typeof
The typeof operator is used to determine the type of a variable. The typeof operator returns the type of a variable, object, function or expression.
typeof "John"; // Returns string
typeof 3.14; // Returns number
typeof NaN; // Returns number
typeof false; // Returns boolean
typeof [1, 2, 3, 4]; // Returns object
typeof { name: "John", age: 34 }; // Returns object
typeof new Date(); // Returns object
typeof function() {}; // Returns function
typeof myCar; // Returns undefined (if myCar is not declared)
typeof null; // Returns object
instanceof
The instanceof operator returns true if the specified object is an instance of the specified object.
var cars = ["Saab", "Volvo", "BMW"];
cars instanceof Array; // Returns true
cars instanceof Object; // Returns true
cars instanceof String; // Returns false
cars instanceof Number; // Returns false
void
The void operator evaluates an expression and returns undefined. This operator is often used to obtain the undefined primitive value, using "void(0)".
// href="javascript:void(0);"
javascript: void 0;
