/

HTML / CSS / JavaScript Tutorial

JavaScript Tutorial: Operators

[this page | pdf | back links]

The main operators that JavaScript recognises are set out below:

 

Arithmetic operators (binary)

 

Operator

Description

More

+

Addition, e.g. if x is 8 then y = x + 2 results in y becoming 10

Here

-

Subtraction, e.g. if x is 8 then y = x - 2 results in y becoming 6

Here

*

Multiplication, e.g. if x is 8 then y = x * 2 results in y becoming 16

Here

/

Division, e.g. if x is 9 then y = x / 2 results in y becoming 4.5

Here

%

Modulus (i.e. division remainder), e.g. if x is 9 then y = x % 4 results in y becoming 1

Here

**

Exponentiation, if x is 8 then y = x ** 5 results in y becoming 32768

Here

 

Arithmetic operators (unary)

 

Operator

Description

More

+

Plus sign, e.g. if x is 8 then then +x represents -8

Here

-

Minus sign, e.g. if x is 8 then then -x represents -8

Here

++

Increment, i.e. add one to the variable. There are technically two different increment operators, the prefix one, and the postfix one. For example, if x is 8 then the statement y = ++x results in x being incremented to 9 and then y assigned this value (i.e. is assigned 9). However, the statement y = x++ involves y being assigned the value of x (i.e. 8) and x then being incremented to 9. For code clarity, some commentators suggest using the statements x = x + 1; y = x; instead of y = ++x and y = x; x = x + 1; instead of y = x++.

Here

--

Decrement, i.e. subtract one from the variable. There are technically two different decrement operators, the prefix one, and the postfix one. For example, if x is 8 then the statement y = --x results in x being decremented to 7 and then y assigned this value (i.e. is assigned 7). However, the statement y = x-- involves y being assigned the value of x (i.e. 8) and x then being decremented to 7. For code clarity, some commentators suggest using the statements x = x - 1; y = x; instead of y = --x and y = x; x = x - 1; instead of y = x--.

Here

 

Arithmetic Assignment operators

 

Operator

Description

More

=

Set equal to, e.g. if x is 5 then y = x results in x remaining 5 and y being set to 5

Here

+=

Set equal to after addition, e.g. if x is 5 and y is 8 then y += x results in x remaining 5 and y = y + x, so y becomes 13

Here

-=

Set equal to after subtraction, e.g. if x is 5 and y is 8 then y -= x results in x remaining 5 and y = y - x, so y becomes 3

Here

*=

Set equal to after multiplication, e.g. if x is 5 and y is 8 then y *= x results in x remaining 5 and y = y * x, so y becomes 40

Here

/=

Set equal to after division, e.g. if x is 5 and y is 8 then y /= x results in x remaining 5 and y = y / x, so y becomes 1.6

Here

%=

Set equal to after modulus (i.e. division remainder) e.g. if x is 5 and y is 8 then y %= x results in x remaining 5 and y = y % x, so y becomes 3

Here

**=

Set equal to after exponentiation, e.g. if x is 5 and y is 8 then y **= x results in x remaining 5 and y = y ** x, so y becomes 32768

Here

 

String operators (binary)

 

Operator

Description

More

+

Concatenation, e.g. if x is "a" then y = x + "b" results in y becoming "ab"

Here

 

String Assignment operators

 

Operator

Description

More

=

Set equal to, e.g. if x is "ab" then y = x results in x remaining "ab" and y being set to "ab"

Here

+=

Set equal to after concatenation, e.g. if x is "a" and y is "b" then y += x results in x remaining "a" and y = y + x, so y becomes "ba"

Here

 

Comparison operators

 

Operator

Description

More

==

Equal to, e.g. if x is 8 then x==8 is true, but x==5 is false

Here

===

Equal to and of the same type, e.g. if x is 8 then x===8 is true but x==="8" is false (as not of the same type, although x=="8" would be true, as the computation involves a conversion of "8" to 8)

Here

!=

Not equal to, e.g. if x is 8 then x!=8 is false, but x!=5 is true

Here

!==

Not equal to or not of the same type, e.g. if x is 8 then x!== "8" is true (as not of the same type), but x!==8 is false

Here

Greater than, e.g. if x is 8 then x>8 is false, but x>5 is true

Here

Less than, e.g. if x is 8 then x<8 is false, but x<11 is true

Here

>=

Greater than or equal to, e.g. if x is 8 then x>=8 is true, but x>11 is false

Here

<=

Less than or equal to, e.g. if x is 8 then x<=8 is true, but x<5 is false

Here

 

Conditional (ternary, i.e. IIF) operator

 

Operator

Description

More

var3 = (boolval) ? var1 : var2

Sets var3 equal to var1 if boolval is true, otherwise sets var3 equal to var2

Here

 

Boolean (i.e. logical) operators

 

Operator

Description

More

&&

Boolean AND operator, e.g. if x is 5 and y is 8 then (x<6 && y>7) is (true && true), i.e. is true

Here

||

Boolean OR operator, e.g. if x is 5 and y is 8 then (x<6 && y>10) is (true || false), i.e. is true

Here

!

Boolean NOT operator, e.g. if x is 5 then !(x==5) is !true, i.e. is false

Here

 

Bitwise operators

 

In JavaScript, these operators apply to (signed) 32-bit numbers. They are particularly fast to compute, because they have very simple analogues in how most computer central processing units (CPUs) operate. However, they can be less easy to follow than using corresponding primitive mathematical operators such as division and modulus remainder (as the latter are not base-2 specific). Application of the bitwise NOT and XOR operators can also be somewhat counter-intuitive for small unsigned numbers because of their application to signed 32-bit numbers.

 

Operator

Description

More

&

Bitwise AND operator, e.g. 6 & 2 in binary notation is 110 & 010 so is 010, i.e. 2 in decimal notation

Here

|

Bitwise OR operator, e.g. 6 | 2 in binary notation is 110 | 010 so is 110, i.e. 6 in decimal notation

Here

~

Bitwise NOT operator, e.g. ~ 6  in binary notation is ~ 110 so is 1(29 times)001, which is interpreted as a negative number so is treated as -7 in decimal notation

Here

^

Bitwise XOR operator

Here

<< 

Bitwise left shift operator, e.g. 6 << 2 in binary notation is 110 shifted 2 bits to left so is 11000, i.e. 24 in decimal notation

Here

>> 

Bitwise right shift operator, e.g. 6 >> 2 in binary notation is 110 shifted 2 bits to the right so is 1, i.e. 1 in decimal notation

Here

 

delete, in, instanceof, typeof, yield, yield* and void operators

 

Operator

Description

More

delete

Deletes a property from an object

Here

in

Returns true if the specified property is in the specified object, otherwise false.

Here

instanceof

Returns true if a specified object is an instance of the specified object type, otherwise returns false

Here

typeof

Returns the type of a variable, object, function or expression

Here

yield

 

 

yield*

 

 

void

Evaluates an expression and then returns undefined

Here

 

Spread and comma/sequence operators

 

Operator

Description

More

Allows an iterable such as an array expression to be expanded where zero or more arguments or elements are expected

 

,

Evaluates each operand from left to right and returns the value of the last operand (e.g. in a for loop). It is not the same as the comma within arrays, objects and function arguments and parameters

 

 

Operator precedence

 

When an expression includes more than one operator it becomes important to identify the precedence given to different operators, i.e. which one is executed first. We set out here details of operator precedence used in JavaScript, as well as some of the more unusual expression components that are formally considered to be operators in the JavaScript (i.e. ECMAScript) specification.

 


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Operators


Desktop view | Switch to Mobile