An operator provides a result by performing an action on a single or more operands.
Arithmetic operators are mathematical symbols that carry out arithmetic operations on operands.
Operator | Description | Example |
---|---|---|
+ | Addition | 100 + 50 = 150 |
- | Subtraction | 100 - 50 = 50 |
* | Multiplication | 100 * 50 = 5000 |
/ | Division | 100 / 50 = 2 |
% | Modulus (Remainder) | 100 % 50 = 0 |
++ | Increment | var val=2; val++; Now val = 3 |
-- | Decrement | var val=2; val--; Now val = 1 |
In programming, assignment operators are used to assign values to variables.
Operator | Description | Example | |
---|---|---|---|
= | Assign | x = y | x = y |
+= | Add and assign | x += y | x = x + y |
-= | Subtract and assign | x -= y | x = x - y |
*= | Multiply and assign | x *= y | x = x * y |
/= | Divide and assign | x /= y | x = x / y |
%= | Modulus and assign | x %= y | x = x % y |
In programming, comparison operators are used to compare values.
Operator | Description |
---|---|
== | 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 |
In programming, logical operations are carried out on boolean values (true or false).
Operator | Description |
---|---|
&& | Logical AND |
|| | Logical OR |
! | Logical Not |
In programming, bitwise operators are used to carry out operations on particular integer bits.
Operator | Description |
---|---|
& | 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
let obj = {};
obj.name = "Vishal";
console.log(obj); // { name: 'Vishal' }
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[0];
console.log(val); //aura
// with destructuring
const [aura] = arr;
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
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
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
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' }
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
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
You may quickly make a new array with all the members of the first array by using the spread operator.
const count1 = [1, 2];
const count2 = [3, 4];
const spreadCount = [...count1, ...count2];
console.log(spreadCount); // [ 1, 2, 3, 4 ]