JS Tutorial
Javascript operators are used to perform different types of mathematical and logical computations.
The Assignment Operator = assigns values
The Addition Operator + adds values
The Multiplication Operator * multiplies values
The Comparison Operator > compares values
The Assignment Operator (=) assigns a value to a variable:
let x = 10;
// Assign the value 5 to x
let x = 5;
// Assign the value 2 to y
let y = 2;
// Assign the value x + y to z:
let z = x + y;
The Addition Operator (+) adds numbers:
let x = 5;
let y = 2;
let z = x + y;
The Multiplication Operator (*) multiplies numbers:
let x = 5;
let y = 2;
let z = x * y;
There are several types of JavaScript operators:
Arithmetic Operators perform arithmetic on numbers.
let a = 3;
let x = (100 + 50) * a;
| Operator | Description |
|---|---|
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| ** | Exponentiation (ES2016) |
| / | Division |
| % | Modulus (Division Remainder) |
| ++ | Increment |
| — | Decrement |
Assignment operators set values for JavaScript variables.
The Addition Assignment Operator (+=) adds a value to a variable.
let x = 10;
x += 5;
| Operator | Example | Same As |
|---|---|---|
| = | x = y | x = y |
| += | x += y | x = x + y |
| -= | x -= y | x = x – y |
| *= | x *= y | x = x * y |
| /= | x /= y | x = x / y |
| %= | x %= y | x = x % y |
| **= | x **= y | x = x ** y |
| Operator | Description |
|---|---|
| == | equal to |
| === | equal value and equal type |
| != | not equal |
| !== | not equal value or not equal type |
| > | greater than |
| < | less than |
| >= | greater than or equal to |
| <= | less than or equal to |
| ? | ternary operator |
All of the comparison operators mentioned above can also be applied to strings:
let text1 = "A";
let text2 = "B";
let result = text1 < text2;
Strings are compared alphabetically:
let text1 = "20";
let text2 = "5";
let result = text1 < text2;
The + operator can also be used to add (concatenate) strings:
let text1 = "John";
let text2 = "Doe";
let text3 = text1 + " " + text2;
You can also use the += assignment operator to add (concatenate) strings:
let text1 = "What a very ";
text1 += "nice day";
The output of text1 will be:
What a very nice day
Adding two integers returns the sum; however, adding a number and a string returns a string:
let x = 5 + 5;
let y = "5" + 5;
let z = "Hello" + 5;
The result of x, y, and z will be:
10
55
Hello5
| Operator | Description |
|---|---|
| && | logical and |
| || | logical or |
| ! | logical not |
| Operator | Description |
|---|---|
| typeof | Returns the type of a variable |
| instanceof | Returns true if an object is an instance of an object type |
Bit operators function with 32-bit numbers.
Any numeric operand in the operation is transformed to a 32-bit value. The result is transformed back into a JavaScript number.
| Operator | Description | 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 |
| << | left shift | 5 << 1 | 0101 << 1 | 1010 | 10 |
| >> | right shift | 5 >> 1 | 0101 >> 1 | 0010 | 2 |
| >>> | unsigned right shift | 5 >>> 1 | 0101 >>> 1 | 0010 | 2 |
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.