JavaScript Operators

An operator is a symbol that specifies an action to perform in an expression, for example, the '+' symbol in the expression '1 + 2' would add the two numbers together to produce a result. This type of operator is known as an arithmetic operator, however, there are also other groups of operators within JavaScript that will be explained below.

Arithmetic Operators

Arithmetic operators allow for mathematical calculations to be carried out within JavaScript.

Operator Description
+ Adds two values together.
- Subtracts one value from another.
* Multiplies two values together.
/ Divides one value by another.
% Calculates the remainder after one value is divided by another.

The following examples show each of these operators in action.

// Variables to use in expressions.
var a = 10,
b = 20,
c = 0;

c = a + b;   // c will equal 30.
c = b - a;   // c will equal 10.
c = a * b;   // c will equal 200.
c = b / a;   // c will equal 2.
c = b % a;   // c will equal 0.

Assignment Operators

Assignment Operators in JavaScript are used to assign values to a variable, the most basic of which is '='. Here, the '=' symbol means 'gets set to' rather than 'equals', so in the expression 'x = y', 'x' gets set to 'y'. The Arithmetic Operator symbols can be combined with '=' to form the Assignment Operators used in the following examples.

var a = 10; // a gets set to 10.

var b = 2;
b += 5; // b gets set to 7.

var c = 10;
c -= 4; // c gets set to 6.

var d = 2;
d *= 2; // d gets set to 4.

var e = 20;
e /= 5; // e gets set to 4.

var f = 5;
f *= 2; // f gets set to 1.

String Operators

The '+' operator can also be used to concatenate two strings or a string and a number.

var a, b, c, d, e;
a = "Hello ";
b = "world";
c = 5;
d = a + b; // d contains the string "Hello world".
e = d + c; // e contains the string "Hello world5".

Relational Operators

Relational operators compare two values and are used, for example, in 'if' statements, which are described in the next section on Decision Making. The result of the comparison is either True or False.

Operator Description
== Evaluates to True if the two values are equal.
=== Evaluates to True if the two values are identical in value and type.
!= Evaluates to True if the two values are not equal.
!== Evaluates to True if the two values are not identical in value or of the same type.
> Evaluates to True if the first value is greater than the second.
< Evaluates to True if the first value is less than the second.
>= Evaluates to True if the first value is greater than or equal to the second.
<= Evaluates to True if the first value is less than or equal to the second.

Logical Operators

Logical operators are used where you want to test more than one condition at the same time, for example, in an 'if' statement. The exception to this is the 'not' operator, which is used to check if a condition is not true. This could be used in conjunction with the other Logical Operators.

Operator Description
&& Evaluates to True if both conditions are True. The equivalent of saying 'and'.
|| Evaluates to True if either condition is True. The equivalent of saying 'or'.
not Evaluates to True if the condition is not True.