# JavaScript Operators

## Table of Content

• Arithmetic Operators
• Assignment Operators
• Comparison (Relational) Operators
• Logical Operators
• Bitwise Operators

An operator provides a result by performing an action on a single or more operands.

## Arithmetic Operators

Arithmetic operators are mathematical symbols that carry out arithmetic operations on operands.

OperatorDescriptionExample
-Subtraction100 - 50 = 50
*Multiplication100 * 50 = 5000
/Division100 / 50 = 2
%Modulus (Remainder)100 % 50 = 0
++Incrementvar val=2; val++; Now val = 3
--Decrementvar val=2; val--; Now val = 1

## Assignment Operators

In programming, assignment operators are used to assign values to variables.

OperatorDescriptionExample
=Assignx = yx = y
+=Add and assignx += yx = x + y
-=Subtract and assignx -= yx = x - y
*=Multiply and assignx *= yx = x * y
/=Divide and assignx /= yx = x / y
%=Modulus and assignx %= yx = x % y

## Comparison Operators

In programming, comparison operators are used to compare values.

OperatorDescription
==Is equal to
===Is equal value and equal type
!=Is not equal
!==Is not equal value or not equal type
>Is greater than
<Is less than
>=Is greater than or equal to
<=Is less than or equal to
?ternary operator

## Logical Operators

In programming, logical operations are carried out on boolean values (true or false).

OperatorDescription
&&Logical AND
||Logical OR
!Logical Not

## Bitwise Operators

In programming, bitwise operators are used to carry out operations on particular integer bits.

OperatorDescription
&AND
|OR
~NOT
^XOR
<<Left shift
>>Right shift
>>>Unsigned right shift

``````let val1 = 2 + 3;        // 5
let val2 = "2" + 3;      // 23
let val3 = "tech" + 3;   // tech3 ``````

### Assigning to properties

``````let obj = {};
obj.name = "Vishal";

console.log(obj);   // { name: 'Vishal' }``````

### Destructuring

Destructuring in JavaScript is a technique for separating data from arrays or objects into different variables. It enables you to quickly allocate the elements or characteristics of an array or object to distinct variables.

``````const arr = ["aura"];

// without destructuring
let val = arr;
console.log(val);  //aura

// with destructuring
const [aura] = arr;
``````

### BigInt

BigInt is a new numeric type in JavaScript that supports numbers with arbitrary precision, allowing you to represent integers with more precision than 53 bits. When working with huge integers that the ordinary Number type cannot represent, this is helpful.

``````const val = 5n + 7n;
console.log(val);  // 12n ``````

### Ternary operator

An if statement in JavaScript can be written more quickly using the ternary operator. A condition, a result for true, and a result for false are the three operands of the if statement, which is shortened to this form.

``````let weight = 80;
let res1 = weight >= 100 ? "overweight" : "normal";
console.log(res1);  // normal

weight = 120;
let res2 = weight >= 100 ? "overweight" : "normal";
console.log(res2);  // overweight ``````

### Typeof operator

In JavaScript, the type of a value is determined by the typeof operator. It gives back a string that denotes the operands type.

``````const count = 9;
const color = "red";
const obj = ["abc", "xyz", "pqr"];
const funName = new Function("usa");

console.log(typeof count);    // number
console.log(typeof color);    // string
console.log(typeof obj);      // object
console.log(typeof funName);  // function  ``````

### Delete operator

JavaScript delete operator is used to remove properties from objects. The object and the property to be erased are the operands for the delete operator. When a property is properly deleted, the delete operator returns true; otherwise, it returns false if the property cannot be deleted or does not exist.

``````const User = {
name: 'Teja',
age: '27'
};
console.log(User);  // { name: 'Teja', age: '27' }

delete User.name;   // Will remove name from User
console.log(User);  // { age: '27' }  ``````

### In operator

JavaScript in operator is used to determine whether an object has a property with a given name. When given an object and a property name as operands, the in operator returns true if the object possesses the specified property and false otherwise.

``````const user = { name: 'Rohit', age: 37, city: 'Mumbai' };
console.log('name' in user);  // true

delete user.name;
console.log('name' in user);  // false ``````

### Instanceof operator

JavaScripts instanceof operator can be used to determine whether an object is an instance of a specific constructor or class. If an object is an instance of the constructor, the instanceof operator returns true; otherwise, it returns false. It accepts an operand and a constructor as input.

``````const element1 = new String("water");
console.log(element1 instanceof String); // true

const element2 = "fire";   // no type specified
console.log(element2 instanceof String); // false

function Color(){}
const c = new Color("Green");
console.log(c instanceof Color);  // true``````

``````const count1 = [1, 2];