JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

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 Demo
- Subtraction a - b Difference of a and b Demo
* Multiplication a * b Product of a and b Demo
** Exponentiation a ** b a to the b power Demo
/ Division a / b Quotient of a and b Demo
% Modulus (Remainder) a % b Remainder of a / b Demo
++ Increment a++ a plus one Demo
-- Decrement a-- a minus one Demo

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 Demo
+= Addition assignment a += b a = a + b Demo
-= Subtraction assignment a -= b a = a - b Demo
*= Multiplication assignment a *= b a = a * b Demo
**= Exponentiation assignment a **= b a = a ** b Demo
/= Division assignment a /= b a = a / b Demo
%= Remainder assignment a %= b a = a % b Demo

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 Demo
=== Identical a === b True if a is equal to b, and they are of the same type Demo
!= Not equal a != b True if a is not equal to b Demo
!== Not identical a !== b True if a is not equal to b, or they are not of the same type Demo
> Greater than a > b True if a is greater than b Demo
< Less than a < b True if a is less than b Demo
>= Greater than or equal to a >= b True if a is greater than or equal to b Demo
<= Less than or equal to a <= b True if a is less than or equal to b Demo

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 Demo
|| Logical OR a || b True if either a or b is true Demo
! Logical NOT !a True if a is not true Demo


JavaScript String Operators

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

Run code

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

Run code

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.

Run code

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:

Run code

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

Run code

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.

Run code

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.

Run code

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.

Run code

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

Run code

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.

Run code