JavaScript tutorial - Operators

Navigation

Skip navigation.

Site search

Site navigation

JavaScript tutorial

Printing

Other tutorials

Operators

The most common operators are mathematical operators; +, -, /, * (add, subtract, divide, multiply) for example. Operators can be split into two groups, comparison operators and assignment or 'action' operators. Comparison operators test to see if two variables relate to each other in the specified way, for example, one variable is a higher number than the other. Other operators perform an action on a variable, such as increasing it by one.

The following table gives those that are most commonly used. The JavaScript 1.3 operators such as the identity operator === are supported by all current browsers, but are not used as often as the others. Old browsers that do not understand them will just produce errors.

JavaScript operators
Operator Uses
+adds two numbers or appends two strings - if more than one type of variable is appended, including a string appended to a number or vice-versa, the result will be a string
-subtracts the second number from the first
/divides the first number by the second
*multiplies two numbers
%divide the first number by the second and return the remainder
=assigns the value on the right to the object on the left
+=the object on the left = the object on the left + the value on the right - this also works when appending strings
-=the object on the left = the object on the left - the value on the right
>number on the left must be greater than the number on the right - this also works with strings and values
<number on the left must be less than the number on the right - this also works with strings and values
>=number on the left must be greater than or equal to the number on the right - this also works with strings and values
<=number on the left must be less than or equal to the number on the right - this also works with strings and values
++increment the number
--decrement the number
==the numbers or objects or values must be equal
!=the numbers or objects or values must not be equal
<<bitwise leftshift
>>bitwise rightshift
&bitwise AND
|bitwise OR
^bitwise XOR
~bitwise NOT
!logical NOT (the statement must not be true)
&&logical AND (both statements must be true)
||logical OR (either statement must be true)
inobject or array on the right must have the property or cell on the left
===the numbers or objects or values must be equal, and must be the same variable type
!==the numbers or objects or values must not be equal, or must not be the same variable type

Note, if you do not set the language="javascript1.2" attribute in the <script> tag, 0 == false == '' and undefined == null. If you do set the language attribute to 'javascript1.2', Mozilla/Firefox and other Gecko browsers (but none of the other major browsers) will change this so that none of these will equate to each other. Since the attribute is deprecated anyway, and the JavaScript versions were never standardised, you should not rely on this behaviour. If you need that behaviour, use the === and !== operators.

There are also a few operators that can also be used like functions:

void
void statement or void(statement) (see the section on writing functions)
typeof
typeof variable or typeof(variable) returns the type (or class) of a variable.
eval
eval(string) interprets a string as JavaScript code.

There are also the 'var', 'new' and 'delete' operators. See the section on variables for examples of their uses. Also see the section on the Math object operators below.

Note that JavaScript has no logical XOR operator. If you need that functionality, see my separate XOR article.

Operator precedence

If you ask JavaScript to perform a calculation using multiple operators, those operators will be evaluated in a specific order. For example 3 + 6 * 7 is calculated as ( 6 * 7 ) + 3 because the * is calculated before the +. The order in which these are evaluated is: * / % + - + (where the second + is appending strings). To change the order in which they are calculated, use parenthesis ( ) as the contents of parenthesis are calculated before the contents outside the parenthesis. For example, 3 + 6 * 7 = 45 but ( 3 + 6 ) * 7 = 63

The Math object methods

In reality, these are methods of the Math object but they are used in place of operators.

Math object methods
Operator What it does
Math.abs(n)Returns the absolute value of n
Math.acos(n)Returns (in radians) cos-1 of n
Math.asin(n)Returns (in radians) sin-1 of n
Math.atan(n)Returns (in radians) tan-1 of n
Math.atan2(n,k)Returns the angle (rads) from cartesian coordinates 0,0 to n,k
Math.ceil(n)Returns n rounded up to the nearest whole number
Math.cos(n)Returns cos n (where n is in radians)
Math.exp(n)Returns en
Math.floor(n)Returns n rounded down to the nearest whole number
Math.log(n)Returns ln(n)
Note, to find log10(n), use Math.log(n) / Math.log(10)
Math.max(a,b,c,...)Returns the largest number
Math.min(a,b,c,...)Returns the smallest number
Math.pow(n,k)Returns nk
Math.random()Returns a random number between 0 and 1
Math.round(n)Returns n rounded up or down to the nearest whole number
Math.sin(n)Returns sin n (where n is in radians)
Math.sqrt(n)Returns the square root of n
Math.tan(n)Returns tan n (where n is in radians)

Last modified: 19 March 2011

  1. Previous
  2. Next
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.