JavaScript Operators and Expressions

Operators can be used both on literal values, such as the numeral 10, and on variables and other objects in JavaScript.

1. Additive Operators

The additive operators includes both addition and subtraction operators. The operators for addition and subtraction are + and – respectively. For example,

4 + 5; // This would be 9.
x + y; // Adds x and y together.
5 - 1; // Results in 4.

The addition operator operates in different ways, depending on the types of the values being added. When adding two strings, the addition operator concatenates the left and right arguments.

2. Multiplicative Operators

Multiplicative operators perform multiplication and division. The multiplication operator (*) multiplies two numbers, whereas the division operator (/) divides numbers. For example,

var mult = 2 * 2;
var divisi = 4 / 2;

The multiplicative operators include the modulo operator, which is indicated by the percent sign (%). The modulo operator yields the remainder of the division of two numbers. For example, the modulo of 4 divided by 3 is 1, as:

var mod = (4 % 3);

3. Bitwise Operators

Bitwise operators include AND, OR, XOR, NOT, Shift Left, Shift Right With Sign, and Shift Right With Zero Fill. Each operator is represented by one or more characters.

  • & AND
  • | OR
  • ^ XOR
  • ~ NOT
  • << Shift Left
  • >> Shift Right With Sign
  • >>> Shift Right With Zero Fill

4. Equality Operators

Equality operators are used to test whether two expressions are the same or different. These operators always return Boolean types: either true or false.

  • == Equal
  • != Not equal
  • === Equal using stricter methods
  • !== Not equal using stricter methods

You can test for equality and inequality in two different ways. These approaches differ in their strictness, the degree of equality they require to determine whether two values are truly equal. The stricter of the two, represented by a triple equals sign (===), requires not only that the values of a given expression are equal but also that the types are identical. For example, the strict test would determine that a string with the value "42" is not equal to a number with the value of 42, whereas the less strict equality test would find them to be equal.

5. Relational Operators

Relational operators test expressions to find out whether they are greater than or less than each other, or whether a given value is in a list or is an instance of a certain type.

  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to
  • in Contained within an expression or object
  • instanceof Is an instance of an object

The in operator is most commonly used to evaluate whether a given property is contained within an object. The in operator searches for the existence of a property and not the value of that property.

The instanceof operator tests whether a given expression, usually a variable, is an instance of the object or a particular data type.

6. Unary Operators

Unary operators have a single operand or work with a single expression in JavaScript.

  • delete Removes a property
  • void Returns undefined
  • typeof Returns a string representing the data type
  • ++ Increments a number
  • -- Decrements a number
  • + Converts the operand to a number
  • - Negates the operand
  • ~ Bitwise NOT
  • ! Logical NOT

7. Assignment Operators

The primary assignment operator is the equals sign (=). This type of operator is known as a simple assignment.

8. Comma Operator

The comma operator separates expressions and executes them in order. Commonly, the comma is used to separate variable declarations, which enables multiple variables to be declared on one line. For example,

var num1, num2, num3;

Alternatively, you can also set values:

var num1=3, num2=7, num3=10;