Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

JavaScript - Operators

Back


Arithmetic Operators

Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation (ES2016)
/ Division
% Modulus (Division Remainder)
++ Increment
-- Decrement

Assignment Operators

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

Logical Assignment Operators

Operator Example Same As    
&&= x &&= y x = x && (x = y)    
`   =` x || = y x = x || (x = y)
??= x ??= y x = x ?? (x = y)    

Comparison Operators

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

Logical Operators

Operator Description    
&& logical and    
`   ` logical or
! logical not    

Type Operators

Operator Description
typeof Returns the type of a variable
instanceof Returns true if an object is an instance of an object type

Bitwise Operators

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      

Conditional (Ternary) Operator

variablename = condition ? value1 : value2;

The Nullish Coalescing Operator (??)


typeof Operator

Target typeof
undefined "undefined"
NaN "number"
null "object"
Array "object"
Date "object"

delete Operator


The Spread Operator: ...


The in Operator

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

The instanceof Operator

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

TOP