You are here: Reference > JavaScript > core > operators

# JavaScript Operators

### Arithmetic Operators

operator description
+ Unary addition operator. Converts a value to a numeric value. Same as the Number constructor.
result = +value;
Sample:
```var num = +false;       // 0
var num = +true;        // 1 (different from zero)
var num = +"10";        // 10
var num = +"010";       // 10
var num = +"-32";       // -32
var num = +"-32.12";    // -32.12
var num = +"0x10";      // 16 (hexadecimal base)
var num = +"zero";      // NaN

// date, milliseconds elapsed since 01 January, 1970 00:00:00
var now = new Date ();
var num = +now;```
Did you find this example helpful?
result = num1 + num2;
Sample:
`var x = 4 + 3;  // after this line x will be 7`
Did you find this example helpful?
++ Increment operator. Increments the value of a numeric expression.
result = num++;
result = ++num;
If the increment operator is used as postfix (num++), the returned value is the value before incrementing, but if it is used as prefix (++num), it returns the incremented value. Regardless of whether the increment operator is used as postfix or prefix, the value of the num variable is increased by one.
Sample:
```var x = 5;
var y = x++; // after this line x will be 6 and y will be 5

var x = 5;
var y = ++x; // after this line x and y will be 6```
Did you find this example helpful?
- Unary negation operator. Negates the value of a numeric expression.
result = -num;
Sample:
```var x = 5;
var y = -x; // after this line y will be -5```
Did you find this example helpful?
- Subtraction operator. Subtracts the value of one numeric expression from another.
result = num1 - num2;
Sample:
`var x = 4 - 3;  // after this line x will be 1`
Did you find this example helpful?
-- Decrement operator. Decrements the value of a numeric expression.
result = num--;
result = --num;
If the decrement operator is used as postfix (num--), the returned value is the value before decrementing, but if it is used as prefix (--num), it returns the decremented value. Regardless of whether the decrement operator is used as postfix or prefix, the value of the num variable is decreased by one.
Sample:
```var z = 5;
var x = z--; // after this line x will be 5 and z will be 4
var y = --z; // after this line y and z will be 3```
Did you find this example helpful?
* Multiplication operator. Multiplies a numeric expression by another.
result = num1 * num2;
Sample:
`var x = 4 * 3;  // after this line x will be 12`
Did you find this example helpful?
/ Division operator. Divides a numeric expression by another.
result = num1 / num2;
Sample:
`var x = 6 / 2;  // after this line x will be 3`
Did you find this example helpful?
% Modulus operator. Returns the integer remainder of division of one number by another.
result = num1 % num2;
Sample:
`var x = 7 % 3;  // after this line x will be 1`
Did you find this example helpful?

### Assignment Operators

operator syntax meaning description
=
x = y
x = y
Assignment operator. Assigns the value of an expression (y) to another (x).
+=
x += y
x = x + y
Addition assigment operator. Performs an addition operation on the value of the variable (x) and the value of the variable (y) and assigns the result to the variable (x).
-=
x -= y
x = x - y
Subtraction assignment operator. Performs a subtraction operation on the value of the variable (x) and the value of the variable (y) and assigns the result to the variable (x).
*=
x *= y
x = x * y
Multiplication assignment operator. Performs a multiplication operation on the value of the variable (x) and the value of the variable (y) and assigns the result to the variable (x).
/=
x /= y
x = x / y
Division assignment operator. Performs a division operation on the value of the variable (x) and the value of the variable (y) and assigns the result to the variable (x).
%=
x %= y
x = x % y
Modulus assignment operator. Performs a modulus operation on the value of the variable (x) and the value of the variable (y) and assigns the result to the variable (x).
<<=
x <<= y
x = x << y
Left Shift assignment operator. Shifts the value of the variable (x) left by the number of specified bits (y) and fills the missing bits on the right side with 0's.
The x <<= y expression is identical to the x *= Math.pow (2, y) expression.
Sample:
```var x = 9;                // binary 1001
x <<= 2;    // x will be 36 (binary 100100)```
Did you find this example helpful?
>>=
x >>= y
x = x >> y
Signed Right Shift assignment operator. Shifts the value of the variable (x) right by the number of specified bits (y) and fills the missing bits on the left side with the sign bit of the original value. The sign bit of a value is the leftmost bit. When the sign bit is 1, then the represented value is negative, else non-negative.
The x >>= y expression is identical to the x = Math.floor (x / Math.pow (2, y)) expression.
Sample:
```    // the binary formats represents the values in 32 bit platforms

var x = 9;
// binary 00000000 00000000 00000000 00001001
x >>= 1;
// x will be 4 (binary 00000000 00000000 00000000 00000100)

var y = -9;
// binary 11111111 11111111 11111111 11110111
y >>= 1;
// y will be -5 (binary 11111111 11111111 11111111 11111011)

var z = -1073741825;
// binary 10111111 11111111 11111111 11111111
z >>= 3;
// z will be -134217729 (binary 11110111 11111111 11111111 11111111)```
Did you find this example helpful?
>>>=
x >>>= y
x = x >>> y
Unsigned Right Shift assignment operator. Shifts the value of the variable (x) right by the number of specified bits (y) and fills the missing bits on the left side with 0's. The sign bit of a value is the leftmost bit. When the sign bit is 1, then the represented value is negative, else non-negative.
Sample:
```    // the binary formats represents the values in 32 bit platforms

var x = 9;
// binary 00000000 00000000 00000000 00001001
x >>>= 1;
// x will be 4 (binary 00000000 00000000 00000000 00000100)

var y = -9;
// binary 11111111 11111111 11111111 11110111
y >>>= 1;
// y will be 2147483643 (binary 01111111 11111111 11111111 11111011)

var z = -1073741825;
// binary 10111111 11111111 11111111 11111111
z >>>= 3;
// z will be 402653183 (binary 00010111 11111111 11111111 11111111)```
Did you find this example helpful?
&=
x &= y
x = x & y
Bitwise AND assignment operator. Performs a bitwise AND operation on the numeric value of a variable (x) and a number (y) and assigns the result to the variable (x).
Sample:
```var x = 9;
x &= 3; // after this line x will be decimal 1 (binary: 1001 & 0011 is 0001)```
Did you find this example helpful?
^=
x ^= y
x = x ^ y
Bitwise XOR assignment operator. Performs a bitwise XOR operation on the numeric value of a variable (x) and a number (y) and assigns the result to the variable (x).
Sample:
```var x = 9;
x ^= 3; // after this line x will be decimal 10 (binary: 1001 ^ 0011 is 1010)```
Did you find this example helpful?
|=
x |= y
x = x | y
Bitwise OR assignment operator. Performs a bitwise OR operation on the value of a variable (x) and a number (y) and assigns the result to the variable (x).
Sample:
```var x = 9;
x |= 3; // after this line x will be decimal 11 (binary: 1001 | 0011 is 1011)```
Did you find this example helpful?

### Bitwise Operators

Bitwise operators work on integers in JavaScript. Floating point values will be converted to integer values (cutting off the decimal part) before the operation.
operator syntax description
<<
x << y
Left Shift operator. Returns the value of an integer (x) shifted left by the number of specified bits (y). This shift operation fills the missing bits on the right side with 0's.
The x << y expression is identical to the x * Math.pow (2, y) expression.
Sample:
`var result = 9 << 2;    // result is 36 (binary: from 1001 to 100100).`
Did you find this example helpful?
>>
x >> y
Signed Right Shift operator. Returns the signed value of an integer (x) shifted right by the number of specified bits (y). This shift operation fills the missing bits on the left side with the sign bit of the value of x.
The sign bit of a value is the leftmost bit. When the sign bit is 1, then the represented value is negative, else non-negative.
The x >> y expression is identical to the Math.floor (x / Math.pow (2, y)) expression.
Sample:
```    // the binary formats represents the values in 32 bit platforms

x = 9 >> 1;
/*
x will be 4,
decimal 9 is binary 00000000 00000000 00000000 00001001
decimal 4 is binary 00000000 00000000 00000000 00000100
*/

y = -9 >> 1;
/*
y will be -5,
decimal -9 is binary 11111111 11111111 11111111 11110111
decimal -5 is binary 11111111 11111111 11111111 11111011
*/

z = -1073741825 >> 3;
/*
z will be -134217729,
decimal -1073741825 is binary 10111111 11111111 11111111 11111111
decimal -134217729 is binary 11110111 11111111 11111111 11111111
*/```
Did you find this example helpful?
>>>
x >>> y
Unsigned Right Shift operator. Returns the unsigned value of an integer (x) shifted right by the number of specified bits (y). This shift operation fills the missing bits on the left side with 0's.
The sign bit of a value is the leftmost bit. When the sign bit is 1, then the represented value is negative, else non-negative.
Sample:
```    // the binary formats represents the values in 32 bit platforms

x = 9 >>> 1;
/*
x will be 4,
decimal 9 is binary 00000000 00000000 00000000 00001001
decimal 4 is binary 00000000 00000000 00000000 00000100
*/

y = -9 >>> 1;
/*
y will be 2147483643,
decimal -9 is binary 11111111 11111111 11111111 11110111
decimal 2147483643 is binary 01111111 11111111 11111111 11111011
*/

z = -1073741825 >>> 3;
/*
z will be 402653183,
decimal -1073741825 is binary 10111111 11111111 11111111 11111111
decimal 402653183 is binary 00010111 11111111 11111111 11111111
*/```
Did you find this example helpful?
&
x & y
Bitwise AND operator. Performs an AND operation on every digit of two integers (x, y) in binary representation. If both integers have 1 at a position, the result will be 1 at that position. Otherwise, the result will be 0 at that position.
Sample:
```var result = 9 & 3;
// after this line result will be decimal 1 (binary: 1001 & 0011 is 0001)```
Did you find this example helpful?
^
x ^ y
Bitwise XOR operator. Performs an XOR operation on every digit of two integers (x, y) in binary representation. If the two integers have a different value at a position, the result will be 1 at that position. Otherwise, the result will be 0 at that position.
Sample:
```var result = 9 ^ 3;
// after this line result will be decimal 10 (binary: 1001 ^ 0011 is 1010)```
Did you find this example helpful?
|
x | y
Bitwise OR operator. Performs an OR operation on every digit of two integers (x, y) in binary representation. If both integers have 0 at a position, the result will be 0 at that position. Otherwise, the result will be 1 at that position.
Sample:
```var result = 9 | 3;
// after this line result will be decimal 11 (binary: 1001 | 0011 is 1011)```
Did you find this example helpful?
~
~ x
Bitwise NOT (complement) operator. Performs a bitwise complement operation on every digit of the integer (x) in binary representation. If the integer has 0 at a position, the result will be 1 at that position. If the integer has 1 at a position, the result will be 0 at that position.
The sign bit of a value is the leftmost bit. When the sign bit is 1, then the represented value is negative, else non-negative. When all bits of a value are 0, the value is 0. When all bits of a value are 1, the value is -1.
Conclusion:
• The ~x expression is identical to the -(x+1) expression for integers x.
• ~~x is always x for integers x.
• x + ~x is always -1 for integers x.
• When x is a non-negative floating point number, the ~~x expression is identical to the Math.floor (x) expression.
Sample:
`var result = ~0;    // after this line result will be decimal -1`
Did you find this example helpful?

### Comparison Operators

operator name description
== Equal Returns true if the left and right expressions are equal. If the types of the two expressions are different, JavaScript attempts to convert them to the same type.
!= Not equal Returns true if the two expressions are not equal. If the types of the two expressions are different, JavaScript attempts to convert them to the same type.
=== Strict equal Returns true if the left and right expressions and their types are both equal.
!== Strict not equal Returns true if the left and right expressions are not equal, or their types are different.
< Less than Returns true if the left expression is lower than the right expression.
<= Less than or equal Returns true if the left expression is lower than or equal to the right expression.
> Greater than Returns true if the left expression is greater than the right expression.
>= Greater than or equal Returns true if the left expression is greater than or equal to the right expression.

### Logical Operators

operator name syntax description
&& logical AND exp1 && exp2 If the value of the first expression (converted to boolean if needed) is false, returns the first expression, otherwise returns the second expression.
Sample:
```true && true;       // returns true
true && false;      // returns false
false && true;      // returns false
false && false;     // returns false
true && 2;          // returns 2
true && 0;          // returns 0
true && "Sam";      // returns "Sam"
false && "Sam";     // returns false
0 && true;          // returns 0
"" && true;         // returns ""
null && true;       // returns null
undefined && true;  // returns undefined```
Did you find this example helpful?
|| logical OR exp1 || exp2 If the value of the first expression (converted to boolean if needed) is true, returns the first expression, otherwise returns the second expression.
Sample:
```true || true;       // returns true
true || false;      // returns true
false || true;      // returns true
false || false;     // returns false
false || 2;         // returns 1
false || 0;         // returns 0
true || "Sam";      // returns true
false || "Sam";     // returns "Sam"
"" || "Sam";        // returns "Sam"
"Sam" || "";        // returns "Sam"
2 || true;          // returns 2
null || true;       // returns true
undefined || true;  // returns true```
Did you find this example helpful?
! logical NOT !exp Returns false if the value of the first expression (converted to boolean if needed) is true, otherwise returns true.
Sample:
```!true;          // returns false
!false;         // returns true
!1;             // returns false
!0;             // returns true
!"Sam";         // returns false
!"";            // returns true
!null;          // returns true
!undefined;     // returns true```
Did you find this example helpful?

### String Operators

operator syntax description
+
str1 + str2
Addition operator. Returns the concatenation of two strings.
Sample:
```var str = "dogs and " + "cats"; // after this line str will be "dogs and cats"
str = "dog" + " and " + "cat";  // after this line str will be "dog and cat"```
Did you find this example helpful?
+=
str1 += str2
Addition assignment operator. Appends the value of one string to another. Same as
str1 = str1 + str2
Sample:
```var str = "dogs";
str += " and cat";  // after this line str will be "dog and cat"```
Did you find this example helpful?

### Member Operators

operator name description
.
Dot notation
Sample:
```var value = object.property;    // returns the value of the property
object.property = value;        // sets the value of the property```
Did you find this example helpful?
[]
Bracket notation
Sample:
```var value = object["property"];     // returns the value of the property
object["property"] = value;         // sets the value of the property```
Did you find this example helpful?

### Special Operators

operator name support description
? : Conditional operator Returns exp1 if condition is true, else returns exp2. Typically used instead of the if...else statement.
Syntax:
condition ? exp1 : exp2
Sample:
```var result = true ? "dog" : "cat";  // after this line result will be "dog"
result = false ? "dog" : "cat";     // after this line result will be "cat"```
Did you find this example helpful?
delete delete operator Removes an element from an Array, or removes a property from an Object.
Sample:
```delete myobj.prop; // removes 'prop' property from 'myobj' object

var arr = ["dog", "cat", "mouse"];
delete arr;
// after this line the 'arr' array will contain only two elements:
// "dog", "mouse"```
Did you find this example helpful?
get get operator Creates a getter method for the specified property.
Syntax:
get prop [funcname]() { [code] };
 prop Required. The name of the property whose value the function returns. funcname Optional. The name of the function to create. code Optional. The code that returns the value of the property.
Sample:
```<head>
<script type="text/javascript">
var o = {
// returns the last inserted element into the arr array
get latest () {
if (this.arr.length > 0) {
return this.arr[this.arr.length - 1];
} else {
return null;
}
},

// adds an element to the end of the arr array
set latest (str) {
this.arr.push (str);
},

arr: ["dog", "cat"]
}

var input = document.getElementById ("myInput");
o.latest = input.value;
}
</script>
<body>
<button onclick="alert (o.latest);">Get the latest element</button>

<br />
<input id="myInput" value="mouse">
<br />
Add the value of the input field to the end of the array
</button>
</body>```
Did you find this example helpful?
in in operator Returns a Boolean value that indicates whether the given property is exists in the given object.
Syntax:
result = property in object
 property Specifies the name of the property to check. object Specifies the object.
Sample:
```var arr = [];
// alerts true, because the Array object has length property```
Did you find this example helpful?
instanceof instanceof operator Returns a Boolean value that indicates whether an object is an instance of the specified object type, or not.
Syntax:
result = object instanceof type
 object Specifies the object to check. type Specifies the object type.
Sample:
```var arr = new Array ();
document.write ("arr = new Array");

// writes true, because the arr variable is an instance of the Array object
document.write ("<br /> arr is an instance of the Array object: ");
document.write (arr instanceof Array);

// writes true, because the Array object is derived from the Object object
document.write ("<br /> arr is an instance of the Object object: ");
document.write (arr instanceof Object);

// writes false,
// because the String object is not a base object of the Array object
document.write ("<br /> arr is an instance of the String object: ");
document.write (arr instanceof String);```
Did you find this example helpful?
new new operator Creates a new instance of the specified object type. It can be used to create an instance of any predefined JavaScript type, or a user defined type. It is always possible to add properties and methods to a newly created object.
Syntax:
objName = new objType ([arguments])
 objName Optional. A reference to the newly created object. objType Required. Specifies the type of the new object. arguments Optional. Parameters for the object.
Sample:
```var arr = new Array ();             // creates a new empty array.
var arr = new Array ("dog", "cat"); // creates a new array with two elements.
var arr = new String ();            // creates a new empty string.
var arr = new String ("dog");       // creates a new string with value: "dog".```
Did you find this example helpful?
set set operator Creates a setter method for the specified property.
Syntax:
set prop [funcname]() { [code] };
 prop Required. The name of the property whose value the function changes. funcname Optional. The name of the function to create. code Optional. The code that changes the value of the property.
Sample:
```<head>
<script type="text/javascript">
var o = {
// returns the last inserted element into the arr array
get latest () {
if (this.arr.length > 0) {
return this.arr[this.arr.length - 1];
} else {
return null;
}
},

// adds an element to the end of the arr array
set latest (str) {
this.arr.push (str);
},

arr: ["dog", "cat"]
}

var input = document.getElementById ("myInput");
o.latest = input.value;
}
</script>
<body>
<button onclick="alert (o.latest);">Get the latest element</button>

<br />
<input id="myInput" value="mouse">
<br />
Add the value of the input field to the end of the array
</button>
</body>```
Did you find this example helpful?
typeof typeof operator Returns the data type of an expression as a String.
Syntax:
typeof [(]expression[)] ;
 expression Required. Specifies a string, variable, keyword, or object whose type is returned.
The following table contains the returned strings by data types:
Object Type typeof string
Boolean "boolean"
Number "number"
String "string"
Function "function"
Any other object "object"
Undefined "undefined"
Null "object"
Sample:
```var func = new Function();
var date = new Date();
var str = "dog";
var num = 5;

typeof func;  // returns "function"
typeof date;  // returns "object";
typeof str;   // returns "string";
typeof num;   // returns "number";```
Did you find this example helpful?
void void operator Evaluates the given expression, and returns undefined. The void operator is useful if you want to evaluate an expression without using the return value.
Sample:
```<!-- If you click this link the color of the text will be red -->
<a href="javascript:void(document.body.style.color='red');"> 