All technological notes.
| Operator | Description |
|---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
** |
Exponentiation (ES2016) |
/ |
Division |
% |
Modulus (Division Remainder) |
++ |
Increment |
-- |
Decrement |
Operators and Operands
The numbers (in an arithmetic operation) are called operands.
The operation (to be performed between the two operands) is defined by an operator.
Remainder
%) returns the division remainder.Incrementing
++) increments numbers.let x = 5;
x++;
let z = x; //x=6
Decrementing
--) decrements numbers.Exponentiation
**) raises the first operand to the power of the second operand.Precedence
| 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 | Example | Same As | ||
|---|---|---|---|---|
&&= |
x &&= y | x = x && (x = y) | ||
| ` | =` | x || = y | x = x || (x = y) | |
??= |
x ??= y | x = x ?? (x = y) |
| Operator | Description |
|---|---|
== |
equal to |
=== |
Strict equality,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 |
| 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 work on 32 bits numbers.
Any numeric operand in the operation is converted into a 32 bit number. The result is converted back to 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 |
The conditional operator assigns a value to a variable based on a condition.
Syntax:
variablename = condition ? value1 : value2;
??)?? operator returns the first argument if it is not nullish (null or undefined).Otherwise it returns the second argument.
arg1 ?? arg2typeof Operatortypeof operator returns the type of a variable, object, function or expression.| Target | typeof |
|---|---|
undefined |
"undefined" |
NaN |
"number" |
null |
"object" |
| Array | "object" |
| Date | "object" |
delete OperatorThe delete operator deletes a property from an object from an object.
The delete operator is designed to be used on object properties. It has no effect on variables or functions.
...... operator expands an iterable into more elements.... operator can be used to expand an iterable into more arguments for function calls.in Operatorin operator returns true if a property is in an object, otherwise false.console.log("\n-------- in Operator --------\n");
const person = { firstName: "John", lastName: "Doe", age: 50 };
console.log("firstName" in person); //true
console.log("age" in person); //true
console.log("address" in person); //false
instanceof Operatorinstanceof operator returns true if an object is an instance of a specified object.console.log("\n-------- instanceof Operator --------\n");
const cars = ["Saab", "Volvo", "BMW"];
console.log(cars instanceof Array); // true
console.log(cars instanceof Object); // true
console.log(cars instanceof String); // false
console.log(cars instanceof Number); // false
function Person(fname, lname) {
this.firstName = fname;
this.lastName = lname;
}
class Car {
constructor(brand) {
this.carname = brand;
}
}
const p1 = new Person("John", "Doe");
const car = new Car("Ford");
console.log(p1 instanceof Person); //true
console.log(car instanceof Person); //false
console.log(car instanceof Car); //true
console.log(p1 instanceof Car); //false