# JavaScript Operators

An operator performs mathematical and logical operations on given operands.

JavaScript has the following types of operators:

• Arithmetic operators
• Assignment operators
• Comparison operators
• Bitwise operators
• Logical operators
• String operators
• Conditional (ternary) operator
• Unary operators
• Relational operators

## JavaScript Arithmetic Operators

Arithmetic operators are used to perform arithmetic on numbers.

An arithmetic operator takes numerical values as their operands and returns a single numerical value.

Syntax Operator Example Definition Demo
`+` Addition a + b Sum of a and b
`-` Subtraction a - b Difference of a and b
`*` Multiplication a * b Product of a and b
`**` Exponentiation a ** b a to the b power
`/` Division a / b Quotient of a and b
`%` Modulus (Remainder) a % b Remainder of a / b
`++` Increment a++ a plus one
`--` Decrement a-- a minus one

## JavaScript Assignment Operators

Assignment operators assign values to JavaScript variables.

The simple assignment operator is equal (`=`), which assigns the value of its right operand to its left operand.

Syntax Operator Example Same As Demo
`=` Assignment a = b a = b
`+=` Addition assignment a += b a = a + b
`-=` Subtraction assignment a -= b a = a - b
`*=` Multiplication assignment a *= b a = a * b
`**=` Exponentiation assignment a **= b a = a ** b
`/=` Division assignment a /= b a = a / b
`%=` Remainder assignment a %= b a = a % b

## JavaScript Comparison Operators

Comparison operators are used to compare two values and returns a Boolean.

Syntax Operator Example Definition Demo
`==` Equal a == b True if a is equal to b
`===` Identical a === b True if a is equal to b, and they are of the same type
`!=` Not equal a != b True if a is not equal to b
`!==` Not identical a !== b True if a is not equal to b, or they are not of the same type
`>` Greater than a > b True if a is greater than b
`<` Less than a < b True if a is less than b
`>=` Greater than or equal to a >= b True if a is greater than or equal to b
`<=` Less than or equal to a <= b True if a is less than or equal to b

The difference between (a`==`b) and (a`===`b) is:

• `==` returns true if a is equal to b
• `===` returns true if a is equal to b, and they are of the same type

Comparison operators can be used in conditional statements to compare values and take action depending on the result.

You will learn about the use of conditional statements later in this tutorial.

## JavaScript 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.

Any numeric operand in the operation is converted into a 32 bit number. The result is converted back to a JavaScript number.

Syntax Operator Example Same as Result Decimal
`&` AND 5 & 1 0101 & 0001 0001  1
`|` OR 5 | 1 0101 | 0001 0101  5
`~` NOT ~ 5  ~0101 1010  10
`^` XOR 5 ^ 1 0101 ^ 0001 0100  4
`<<` Zero fill left shift 5 << 1 0101 << 1 1010  10
`>>` Signed right shift 5 >> 1 0101 >> 1 0010   2
`>>>` Zero fill right shift 5 >>> 1 0101 >>> 1 0010   2

The examples above uses 4 bits unsigned examples. But JavaScript uses 32-bit signed numbers.

Because of this, in JavaScript, ~ 5 will not return 10. It will return -6.

~00000000000000000000000000000101 will return 11111111111111111111111111111010

## JavaScript Logical Operators

Logical operators are used to determine the logic between variables or values.

Logical operators are typically used to combine conditional statements, they return a Boolean value.

Syntax Operator Example Definition Demo
`&&` Logical AND a && b True if both a and b are true
`||` Logical OR a || b True if either a or b is true
`!` Logical NOT !a True if a is not true

## JavaScript String Operators

The `+` operator can also be used to add (concatenate) strings.

The addition assignment operator `+=` can also be used to add (concatenate) strings.

When used on strings, the `+` operator is called the concatenation operator.

## JavaScript 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 ? val1 : val2

If the condition is evaluated to true the val1 will be returned, otherwise val2 will be returned.

You can use the conditional operator anywhere you would use a standard operator.

The above statement assigns the value "adult" to the variable status if age is eighteen or more. Otherwise, it assigns the value "minor" to status.

## JavaScript Unary operators

A unary operation is an operation with only one operand.

There are three unary operators in JavaScript:

Operator Description
`delete` Deletes an object, an object's property, or an element at a specified index in an array
`typeof` Returns the type of a variable
`void` Specifies an expression to be evaluated without returning a value

## The delete operator

The `delete` operator deletes an object, an object's property, or an element at a specified index in an array.

The delete operator returns true if the operation is possible; it returns false if the operation is not possible.

If the delete operator succeeds, it sets the property or element to undefined:

When the delete operator removes an array element, that element is no longer in the array.

Note: When you delete an array element, the array length is not affected.

## The typeof operator

The `typeof` operator returns the type of a variable or an expression.

Note: The `typeof` operator returns "object" for arrays because in JavaScript arrays are objects.

Here is a list of the return values for the `typeof` operator:

Type String Returned by typeof
Number "number"
String "string"
Boolean "boolean"
Object "object"
Function "function"
Undefined "undefined"
Null "object"

## The void operator

The `void` operator specifies an expression to be evaluated without returning a value.

The following code creates a hypertext link that does nothing when the user clicks it.

## JavaScript Relational operators

A relational operator compares its operands and returns a Boolean value based on whether the comparison is true.

There are two relational operators in JavaScript:

Operator Description
`in` Returns true if the specified property is in the specified object
`instanceof` Returns true if an object is an instance of an object type

## The in operator

The `in` operator returns true if the specified property is in the specified object.

When using `in` operator with Array, you must specify the index number, not the value at that index.

## The instanceof operator

The `instanceof` operator returns true if the specified object is of the specified object type.

Use `instanceof` operator when you need to confirm the type of an object at runtime.