Operators

Operators are the symbols between values that allow different operations like addition, subtraction, multiplication, and more.

JavaScript has dozens of operators, so let’s focus on the ones you’re likely to see most often.

Arithmetic

The + operator adds two numbers.

Editable Code

1 + 2;

The - operator subtracts one number from another.

Editable Code

50 - 15;

The * operator multiplies two numbers. Notice it’s an asterisk and not the × symbol commonly used in math.

Editable Code

3 * 12;

The / operator divides one number by another. Notice it’s a forward slash and not the ÷ symbol commonly used in math.

Editable Code

12 / 4;

JavaScript expressions follow a order of operations, so even though + is written first in the following example, the multiplication happens first between the last two numbers and *.

Editable Code

1 + 100 * 5;

If want more control over the order, that’s where the grouping operator comes in handy.

Grouping

() operator groups other values and operations. Code located between parentheses evaluates first as JavaScript solves each operation moving from left to right.

Adding the grouping operator to the previous example causes 1 + 100 to evaluate first.

Editable Code

(1 + 100) * 5;

Concatenation

The + operator can also concatenate strings, which is another way of saying it can add them together.

Editable Code

"news" + "paper";

Assignment

The = operator assigns values. It’s used for setting the value of variables.

Example

var dinner = "sushi";