Assignment operators
An assignment operator assigns a value to its left operand based on the value of its right operand.
Name | Shorthand operator | Meaning |
---|---|---|
Assignment | x = f() | x = f() |
Addition assignment | x += f() | x = x + f() |
Subtraction assignment | x -= f() | x = x - f() |
Multiplication assignment | x *= f() | x = x * f() |
Division assignment | x /= f() | x = x / f() |
Remainder assignment | x %= f() | x = x % f() |
Exponentiation assignment | x **= f() | x = x ** f() |
Left shift assignment | x <<= f() | x = x << f() |
Right shift assignment | x >>= f() | x = x >> f() |
Unsigned right shift assignment | x >>>= f() | x = x >>> f() |
Bitwise AND assignment | x &= f() | x = x & f() |
Bitwise XOR assignment | x ^= f() | x = x ^ f() |
Bitwise OR assignment | x |= f() | x = x | f() |
Logical AND assignment | x &&= f() | x && (x = f()) |
Logical OR assignment | x ||= f() | x || (x = f()) |
Nullish coalescing assignment | x ??= f() | x ?? (x = f()) |
Comparison operators
A comparison operator compares its operands and returns a logical value based on whether the comparison is true.
Operator | Description | Examples returning true |
---|---|---|
Equal (== ) | Returns true if the operands are equal. | 3 == var1 "3" == var1 3 == '3' |
Not equal (!= ) | Returns true if the operands are not equal. | var1 != 4 |
Strict equal (=== ) | Returns true if the operands are equal and of the sametype. See also Object.is andsameness in JS. | 3 === var1 |
Strict not equal (!== ) | Returns true if the operands are of the same type but not equal, or are of different type. | var1 !== "3" |
Greater than (> ) | Returns true if the left operand is greater than the right operand. | var2 > var1 |
Greater than or equal ( >= ) | Returns true if the left operand is greater than or equal to the right operand. | var2 >= var1 |
Less than ( < ) | Returns true if the left operand is less than the right operand. | var1 < var2 |
Less than or equal ( <= ) | Returns true if the left operand is less than or equal to the right operand. | var1 <= var2 |
Arithmetic operators
An arithmetic operator takes numerical values (either literals or variables) as their operands and returns a single numerical value.
Operator | Description | Example |
---|---|---|
Remainder (% ) | Binary operator. Returns the integer remainder of dividing the two operands. | 12 % 5 returns 2. |
Increment (++ ) | Unary operator. 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 postfix operator ( x++ ), returns the value ofits operand before adding one. | If x is 3, then ++x sets x to 4and returns 4, whereas x++ returns 3 and, only then, sets x to 4. |
Decrement ( -- ) | Unary operator. Subtracts one from its operand. The return value is analogous to that for the increment operator. | If x is 3, then --x sets x to 2and returns 2, whereas x-- returns 3 and, only then, sets x to 2. |
Unary negation (- ) | Unary operator. Returns the negation of its operand. | If x is 3, then -x returns -3. |
Unary plus (+ ) | Unary operator. Attempts to convert the operand to a number, if it is not already. | +"3" returns 3 .+true returns 1 . |
Exponentiation operator (** ) | Calculates the base to the exponent power,that is, base^exponent | 2 ** 3 returns 8 .10 ** -1 returns 0.1 . |
Bitwise operators
A bitwise operator treats their operands as a set of 32 bits (zeros and ones), rather than as decimal, hexadecimal, or octal numbers. For example, the decimal number nine has a binary representation of 1001. Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values.
Operator | Usage | Description |
---|---|---|
Bitwise AND | a&b | Returns a one in each bit position for whichthe corresponding bits of both operands are ones. |
Bitwise OR | a|b | Returns a zero in each bit position for whichthe corresponding bits of both operands are zeros. |
Bitwise XOR | a^b | Returns a zero in each bit position for whichthe corresponding bits are the same.[Returns a one in each bit position for which the corresponding bits are different.] |
Bitwise NOT | ~a | Inverts the bits of its operand. |
Left shift | a<<b | Shifts a in binary representation b bits to the left, shifting in zeros from the right. |
Sign-propagating right shift | a>>b | Shifts a in binary representation b bits to the right, discarding bits shifted off. |
Zero-fill right shift | a>>>b | Shifts a in binary representation b bits to the right, discarding bits shifted off, and shifting in zeros from the left. |
Logical operators
Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value.
Operator | Usage | Description |
---|---|---|
AND (&& ) | expr1 && expr2 | Returns expr1 if it can be converted to false ;otherwise, returns expr2 . Thus, when used with Boolean values, && returns true if bothoperands are true; otherwise, returns false . |
OR (|| ) | expr1 || expr2 | Returns expr1 if it can be converted to true ; otherwise, returns expr2 . Thus, when used with Boolean values, || returns true if either operand istrue; if both are false, returns false . |
NOT (! ) | !expr | Returns false if its single operand that can be converted to true ; otherwise, returns true . |
BigInt operators
Most operators that can be used between numbers can be used between BigInt
values as well.
Ex:
1 2 3 4 5 6 | // BigInt addition const a = 1n + 2n; // 3n // Division with BigInts round towards zero const b = 1n / 2n; // 0n // Bitwise operations with BigInts do not truncate either side const c = 40000000000000000n >> 2n; // 10000000000000000n |
One exception is unsigned right shift (>>>
), which is not defined for BigInt values. This is because a BigInt does not have a fixed width, so technically it does not have a “highest bit”.
1 | const d = 8n >>> 2n; // TypeError: BigInts have no unsigned right shift, use >> instead |
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.
Ex:
1 | console.log("my " + "string"); // console logs the string "my string". |
The shorthand assignment operator +=
can also be used to concatenate strings.
Ex:
1 2 | let 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
is true, the operator has the value of val1
. Otherwise it has the value of val2
.Ex:
1 | const status = age >= 18 ? "adult" : "minor"; |
Comma operator
The comma operator (,
) evaluates both of its operands and returns the value of the last operand. This operator is primarily used inside a for
loop, to allow multiple variables to be updated each time through the loop.
Ex:
1 2 3 4 5 6 7 | const x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const a = [x, x, x, x, x]; for (let i = 0, j = 9; i <= j; i++, j--) { // ^ console.log(`a[${i}][${j}]= ${a[i][j]}`); } |
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[propertyKey];
delete objectName[index];
delete
operator succeeds, it removes the property from the object. Trying to access it afterwards will yield undefined
. The delete
operator returns true
if the operation is possible; it returns false
if the operation is not possible.1 2 3 4 | delete Math.PI; // returns false (cannot delete non-configurable properties) const myObj = { h: 4 }; delete myObj.h; // returns true (can delete user-defined properties) |
typeof
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. The parentheses are optional.
Ex:
1 2 3 4 5 | const myFun = new Function("5 + 2"); const shape = "round"; const size = 1; const foo = ["Apple", "Mango", "Orange"]; const today = new Date(); |
The typeof
operator returns the following results for these variables:
1 2 3 4 5 6 7 8 | typeof myFun; // returns "function" typeof shape; // returns "string" typeof size; // returns "number" typeof foo; // returns "object" typeof today; // returns "object" typeof doesntExist; // returns "undefined" typeof true; // returns "boolean" typeof null; // returns "object" |
void
The void
operator specifies an expression to be evaluated without returning a value. expression
is a JavaScript expression to evaluate. The parentheses surrounding the expression are optional, but it is good style to use them to avoid precedence issues.
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
is a string, numeric, or symbol expression representing a property name or array index, and objectName
is the name of an object.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // Arrays const trees = ["redwood", "bay", "cedar", "oak", "maple"]; 0 in trees; // returns true 3 in trees; // returns true 6 in trees; // returns false "bay" in trees; // returns false // (you must specify the index number, not the value at that index) "length" in trees; // returns true (length is an Array property) // built-in objects "PI" in Math; // returns true const myString = new String("coral"); "length" in myString; // returns true // Custom objects const mycar = { make: "Honda", model: "Accord", year: 1998 }; "make" in mycar; // returns true "model" in mycar; // returns true |
instanceof
The instanceof
operator returns true
if the specified object is of the specified object type. The syntax is:
where objectName
is the name of the object to compare to objectType
, and objectType
is an object type, such as Date
or Array
.
Ex:
1 2 3 4 | const theDay = new Date(1995, 12, 17); if (theDay instanceof Date) { // statements to execute } |