JavaScript Operators
Assignment operators
Comparison operators
Arithmetic operators
Logical operators
String operators
Conditional (ternary) operator
Unary operators
Relational operators
Assignment operators
Name Shorthand operator Meaning
Assignment x=y x=y
Addition assignment x += y x=x+y
Subtraction assignment x -= y x=x-y
Multiplication
x *= y x=x*y
assignment
Division assignment x /= y x=x/y
Remainder assignment x %= y x=x%y
Exponentiation
x **= y x = x ** y
assignment
Comparison operators
Operator Description
Equal (==) Returns true if the operands are equal.
Not equal (!=) Returns true if the operands are not equal.
Returns true if the operands are equal and of the
Strict equal (===)
same type.
Returns true if the operands are of the same type but
Strict not equal (!==)
not equal, or are of different type.
Returns true if the left operand is greater than the
Greater than (>)
right operand.
Returns true if the left operand is greater than or
Greater than or equal (>=)
equal to the right operand.
Returns true if the left operand is less than the right
Less than (<)
operand.
Returns true if the left operand is less than or equal
Less than or equal (<=)
to the right operand.
Arithmetic operators
Operator Description
Remainder (%) Returns the integer remainder of dividing the two operands.
Adds one to its operand. If used as a prefix operator (++x),
returns the value of its operand after adding one; if used as a
Increment (++)
postfix operator (x++), returns the value of its operand before
adding one.
Subtracts one from its operand. The return value is analogous to
Decrement (--)
that for the increment operator.
Unary negation (-) Returns the negation of its operand.
Unary plus (+) Attempts to convert the operand to a number, if it is not already.
Exponentiation Calculates the base to the exponent power, that
operator (**) is, base^exponent
Logical operators
Operator Usage
Logical AND (&&) expr1 && expr2
Logical OR (||) expr1 || expr2
Logical NOT (!) !expr
String operators
In addition to the comparison operators,
which can be used on string values, the
concatenation operator (+) concatenates two
string values together, returning another
string that is the union of the two operand
strings.
console.log('my ' + 'string'); // console logs the
string "my string".
var mystring = 'alpha'; mystring += 'bet'; //
evaluates to "alphabet" and assigns this value
to mystring.
Conditional (ternary) operator
The conditional operator is the only JavaScript operator
that takes three operands. The operator can have one of
two values based on a condition. The syntax is:
condition ? val1 : val2
If condition is true, the operator has the value of val1.
Otherwise it has the value of val2. You can use the
conditional operator anywhere you would use a standard
operator.
For example,
var status = (age >= 18) ? 'adult' : 'minor';
Unary operators
A unary operation is an operation with only one operand.
delete
The delete operator deletes an object's property. The
syntax is:
delete object.property;
where object is the name of an object, property is an
existing property, and propertyKey is a string or symbol
referring to an existing property. If the delete operator
succeeds, it removes the property from the object. Trying
to access it afterwards will yield undefined.
Unary operators
typeof
The typeof operator is used in either of the
following ways:
typeof operand
typeof (operand)
The typeof operator returns a string indicating
the type of the unevaluated operand. operand
is the string, variable, keyword, or object for
which the type is to be returned.
Relational operators
A relational operator compares its operands and
returns a Boolean value based on whether the
comparison is true.
in
The in operator returns true if the specified property
is in the specified object. The syntax is:
propNameOrNumber in objectName
where propNameOrNumber is a string, numeric, or
symbol expression representing a property name or
array index, and objectName is the name of an object.
Spread operator
Spread syntax (...) allows an iterable such as
an array expression or string to be expanded
in places where zero or more arguments (for
function calls) or elements (for array literals)
are expected, or an object expression to be
expanded in places where zero or more key-
value pairs (for object literals) are expected.
Spread syntax can be used when all elements
from an object or array need to be included in
a list of some kind.
Rest operator
Rest syntax looks exactly like spread syntax.
In a way, rest syntax is the opposite of spread
syntax. Spread syntax "expands" an array into
its elements, while rest syntax collects
multiple elements and "condenses" them into
a single element.
Destructuring
For more complex assignments, the destructuring
assignment syntax is a JavaScript expression that makes it
possible to extract data from arrays or objects using a syntax
that mirrors the construction of array and object literals.
var foo = ['one', 'two', 'three'];
// without destructuring
var one = foo[0];
var two = foo[1];
var three = foo[2];
// with destructuring
var [one, two, three] = foo;