JavaScript Operators

In JavaScript the are binary operators and unary operators. There is also one ternary operator called the conditional operator.

A binary operator requires an operand to the left, an operator in the middle and another operand to the right. An example of a binary operator would be:

newSum += oldSum;

In the above example, new Sum is assigned the value of newSum + the value of oldSum. It is a sort of shorthand notation.

A unary operator requires only a single operand. An example of a urnary operator would be:

x++

In the above example, the value of x is increased by 1.

Javascript has the following types of operators:

Three dot cartouche

Assignment Operators

With assignment operators, the left operand is assigned a value based on the type of operator and the value of the right operand. These operators are shorthand methods to make code faster to type and take up less file space. For the sake of simplicity, this article will not be addressing bitwise operators, so will not show the shorthand versions of bitwise operations.

OperatorEquivalent
 a += b  a = a + b
 a -+ b  a = a - b
 a *= b  a = a * b
 a /= b  a = a / b
 a %= b  a = a % b

Three dot cartouche

Comparison Operators

Comparison operators compare the left and right operand and return a Boolean value based on whether the test is true or false. Comparison operators do not change the value of either of the operands, they smply test if the expression is true and return the value of true.

OperatorNameResult
== equal returns true if the operands are of equal value
!= not equal returns true if the operands are not of equal value
=== strict equal returns true if the operands are equal and of the same type
!== strict not equal returns true if the operands are not equal and/or not of the same type
greater than returns true if the value of the left operand is greater than the value of the right operand
< less than returns true if the value of the left operand is less than the value of the right operand
>= greater than or equal to returns true if the value of the left operand is greater than or equal to the value of the right operand
<= less than or equal to returns true if the value of the left operand is less than or equal to the value of the right operand

Three dot cartouche

Arithmetic operators

Arithmetic operators take numerical values as their operands and return a single numerical value. Arithmetic operators add, subtract, multiple, divide or find the remainder for variables or literals. One or more arithmetic operator can be used in a statement. Multiplication and division will come first, then addition and subtraction if multiple operators are used in a statement. Operands can be grouped using parentheses to calculate that value before calculating the other values.

operatornameresult
+ addition adds two or more variables and or literals to return a single numerical value
- subtraction subtracts the operand on the right from the operand on the left to return a single numerical value
* multiplication multiplies the operands together to return a single numerical value
/ division divides the operand on the left by the operand on the right to return a single numerical value
% remainder returns the remainder of dividing the operand on the left by the operator on the left
y++ postfix increment increases the operand by 1 but returns the value of the operand prior to incrementing
++y prefix increment increases the operand by 1 returns the value of the operand after incrementing
y-- postfix decrement decreases the operand value by 1 but returns the value of the operand before decrementing
--y prefix decrement decreases the operand value by 1 and returns the value after decrementing
- unary negation precedes the operand and returns the negative value of the operand
+ unary plus attempts to turn the operand into a number

Examples of each:

1 + 3 // returns 4
4 - 3 // returns 1
7 * 5 // returns 35
40 / 4 // returns 10
45 % 6 // returns 3

var x = 8;
y = x++ // result: Y = 8 but x = 9

var x = 8;
y = ++x // result: y = 9 and x = 9

var x = 8;
++x; // result: x = 9;

var a = 7;
b = a--; // result: b = 7 but a = 6

var a = 7;
b = --a; // result: b = 6 and z = 6

var a = 7;
--a; // result: a = 6

var m = 8;
m = -m // result m = -8

var test = "8";
x = +test // result: x has a numerical value of 8

Three dot cartouche

Logical operators

Logical Operators usually return a Boolean value of true or false. When using the logical operators of && or || they can also return the value of one of the specified operands. Logical operators are used in conditional statements to test if two or more conditions are true.

OperatorExampleDescription
&& condition1 && condition2 Returns true if both conditions are true
|| condition1 || condition2 Returns true if one or both conditions are true
! !condition1 Returns true if the condition is NOT true

Following is an example of a logical operator being used:

if (totalParticipants > 30 && totalParticipants < 100)){
  // code goes here
}

Three dot cartouche

String operators

The plus sign + can be used to combine two or more strings.

var clientName = "William Peabody";
var greeting = "Welcome" + clientName;