Operators in Javascript

Javascript

Operators in JavaScript are symbols or keywords that perform operations on operands. These operations can be anything from basic arithmetic calculations to more complex logical evaluations.

JavaScript has lots of helpful operators, and we can group them into different types, each with its own special job.

Types of Operators

1. Arithmetic Operators

Arithmetic operators are the mathematicians of JavaScript, responsible for performing various mathematical operations on numeric values. These operations include ‘+’, ‘-’, ‘*’, and ‘/’.

let x = 10;
let y = 5;

let sum = x + y; // Addition
let difference = x - y; // Subtraction
let product = x * y; // Multiplication
let quotient = x / y; // Division

2. Comparison (Relational) Operators

These operators determine the relationship between two values and return a Boolean result. Common comparison operators include greater than (>), less than (<), equal to (==), and not equal to (!=).

let a = 5;
let b = 10;

console.log(a > b); // Output: false
console.log(a < b); // Output: true

3. Bitwise Operators

Bitwise operators work with the binary form of numbers, dealing with the 0s and 1s in their makeup. It includes AND (&), OR (|), XOR (^), and NOT (~).

let m = 5; // Binary: 0101
let n = 3; // Binary: 0011

console.log(m & n); // Bitwise AND: 1
console.log(m | n); // Bitwise OR: 7

4. Logical Operators

Think of logical operators as tools that help computers make decisions. They include logical AND (&&), logical OR (||), and logical NOT (!).

let p = true;
let q = false;

console.log(p && q); // Logical AND: false
console.log(p || q); // Logical OR: true

5. Assignment Operators

Assignment operators are responsible for assigning values to JavaScript variables. They include the simple assignment operator (=) and compound assignment operators like +=, -=, and more.

let r = 10;

r += 5; // Equivalent to: r = r + 5;

6. Special Operators

JavaScript features an array of special operators, each with a distinct purpose. Let’s explore some of these special operators:

a. Conditional (Ternary) Operator (?:)

The conditional operator, often referred to as the ternary operator, provides a concise way to write conditional statements.

let isSunny = true;
let weatherMessage = isSunny ? "Enjoy the sun!" : "Don't forget your umbrella!";
console.log(weatherMessage);

b. Comma Operator ,

The comma operator allows you to include multiple expressions in a single statement, evaluating them from left to right.

let a = 5, b = 10, c = 15;
console.log(a, b, c); // Output: 5 10 15

c. Delete Operator delete

The delete operator removes a property from an object.

let car = { brand: "Toyota", model: "Camry" };
delete car.model;
console.log(car); // Output: { brand: "Toyota" }

d. in Operator

The ‘in’ operator checks if a specified property is in an object.

let fruits = { apple: "red", banana: "yellow" };
console.log("banana" in fruits); // Output: true

e. instanceof Operator

The instanceof operator checks if an object is an instance of a specific class or constructor.

function Car() {}
let myCar = new Car();
console.log(myCar instanceof Car); // Output: true

f. new Operator

The new operator creates an instance of a user-defined object type or one of the built-in object types.

function Person(name) {
  this.name = name;
}

let john = new Person("John");
console.log(john.name); // Output: John

g. typeof Operator

The typeof operator returns a string indicating the type of a variable.

let age = 25;
console.log(typeof age); // Output: number

h. void Operator

The void operator evaluates an expression and then returns undefined.

let result = void 0;
console.log(result); // Output: undefined

i. yield Operator

The ‘yield’ operator is used in generator functions to pause and resume the execution of a function.

function* generateNumbers() {
  yield 1;
  yield 2;
  yield 3;
}

let iterator = generateNumbers();

console.log(iterator.next().value); // Output: 1

Operator Precedence in Javascript

Think of operator precedence as a set of rules that helps the computer decide which math operation to do first. Here is a table of operator precedence in JavaScript, arranged from the highest to the lowest precedence.

PrecedenceOperator TypesIndividual Operators
1Member Access.
2Computed Member Access[]
3Call, Create Instance(), new
4Increment, Decrement++, –
5Logical NOT, Bitwise NOT!, ~
6Positive, Negative+, -
7Exponentiation**
8Multiplication, Division, Remainder*, /, %
9Addition, Subtraction+, -
10Bitwise Shifts<<, >>, >>>
11Relational, Instanceof, in<, >, <=, >=, instanceof, in
12Equality== , !=, ===, !==
13Bitwise AND&
14Bitwise XOR^
15Bitwise OR`
16Logical AND&&
17Logical OR`
18Conditional (Ternary) Operator? :
19Assignment=, +=, -=, *=, /=, %= and others
20Comma,