JavaScript Operators and testing tool

The C language has transmitted - as to many other languages - its operators to JavaScript. The main symbols that contains a keyboard are used to support the range of possible operations in an expression.

Thus results of the test tool below are also valid for other languages using C operators: PHP, Java, C #. However, the result of the division could be different for typed languages.

Checking an operator

Enter numbers and an operator and click on Test for the operation.

Operand Result

Checking an expression

You can use the x variable to test a simple or compound assignment. It is set to zero by the Clear button. Any variable, y for example, can also be used. So a same variable change on a series of assignments such as: y = 5 then y + = 12 etc..
To reset variables other than x, reload the page.


Arithmetic operators

There are binary operators, except negation, and thus have the form:

x = a + b 
+    addition
-    substraction
*    multiplication
/    division
%    modulus. Returns the reminder of the division of two  numbers.
** exponentiation, replaces Math.pow (since 2017). ++ increment, a simpler way to add 1. -- decrement, substracts 1. - negate, generate the negative of a number. In the form x = -y

Note that the division can produce an integer if there is no remainder, or a real number otherwise. In typed languages, this may be different.

String have two operators:

+    concatenates two  strings:   a = str1 + str2
+=  concatenates another string:  a += str2

Comparison operators

These binary operators are used in conditional tests, to compare numbers or string and return true if the comparison succeeds or false if it fails.
When the operands have different types, they are converted before the comparison.

if(x == 5) ...  
==   equal
<     less
>     greater
<=   less or equal
>=   greater or equal
!=    not equal

Two strings are equal if they have the same size and the same characters at the same positions.
Two variables corresponding to objects are equal if it is the same object assigned to two different variables.

Strict operators

When the operands of a strict comparison are of different types, the result is always false, regardless of the values.

===  strict equality
!==   strict difference

The codes null and undefined are equal, but not strictly equal.

There were no other strict operators in the version 1.5 of JavaScript.

Logical operators

They are used in conditions, as are operators of comparison:

if(a && b) ... 
&&    logical and. true if the two operands are true.
|| logical or. true if one of the two operands is true.
! logical not. true if the operand is false.

Assignment operators

A simple assignment is made with the = operator:

a = 5 

But we can combine assignment with other operations when the assigned variable is one of the two operands, for example:

a += b       is similar to      a = a + b 
+=    add a value.
-=    substract.
*=    multiplied by the operand.  Ex: a *= 3, the value was 2 it is now 6.
/=    divided by the operand. 
<<=  left shift and insert zeroes at right. Ex: a <<= 2. if the value was 10, it becomes 40 after two shifts.
>>=  right shift. 
>>>= right shift, ignore bit of sign .
&=    binary and.
|=      binary or.
^=     binary xor.  Ex: a ^=2, if the value was  4, it is 16. 

Bitwise operators

They can perform operations on the binary representation of numbers. Thus 0110 & 0010 returns 0010.

&      bitwise and.
|       bitwise or.
^      bitwise xor.
~     invert the values of the bits.
a << b   left shift  b times. Ex:   x = a << b. Si the value of a was 0001 and b 10, the results is 0100.
a >> b  right shift  b times, the leftmost bit is preserved.
a >>> b  right shift  b times, the leftmost bit is shifted too and replaced by a zero.

The difference between >> and >>> is that the leftmost bit, which is the sign bit is preserved in the first case and not in the second. This helps preserve the sign, while in the second case the variable is supposed to be used as a storage of bits and the leftmost bit does not have a role of sign.

Other operators

They have different roles in the language depending the context.

.         the dot associates a member to an object.
[ ]       square bracket enclose indices. 
()        parentheses group expressions.
,         the comma is a separator.
? :       this construct means for exp ? action1 : action2, if exp is true, execute action1 else action2.
=> lambda, replace function() { } new before a constructor, create an object. this designate an internal variable in an object. in inclusion. For x in y, the condition is true if x is included among elements of y. delete remove an object, a variable. instanceof x instanceof y returns true if the object x is an instance of y. typeof typeof x display a string that shows the type of x.


The annotation operator defined in Asm.js (subset of JavaScript) is a logical operator used by the language to infer the type of a variable. When you write:

 x =  x | 0

This means that x is x or zero, and zero implies an integer type, it would be 0.0 for a float. There are only numerical types in Asm.js.

Precedence of JavaScript operators

You may provide an expression without parentheses thanks to the precedence that determines which operands are firstly handled by an operator.
Thus (x * y) + z is equivalent to x * y + z, because * has precedence over +.

In order of precedence:

.   []  dot and square bracket have higher priority over any other operators and then...
() new
!   ~  - + ++ -- typeof void delete:  all unary operators
* / %
+ -
<< >> >>>
< > <= >= in instanceof
== != === !==
=   and other assignment operators 
,    the comma has the lowest priority.

See also

© 2008-2017