JavaScript Operators

In JavaScript, an operator is a distinctive symbol or keyword that functions on one or multiple operands to yield a result. It holds significant importance in managing the flow and handling of data within the language.

Types of JavaScript Operators

There are various operators that JavaScript supports. Such as:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Ternary Operators
  • Delete Operators
  • String Operators
  • Typeof Operators
  • Instanceof Operators
  • Chaining Operators
  • Comma Operators
  • 1. Arithmetic Operators

In JavaScript, arithmetic operators facilitate various mathematical computations, such as addition, subtraction, multiplication, and more.

Operator Description Example
+ (Addition) It adds two operands. 10 + 2 // 12
- (Subtraction) Subtracts the second number from the first. 10 - 2 // 8
* (Multiplication) It multiplies both operands. 5 * 2 // 10
/ (Division ) It divides two operands. 10 / 2 // 5
% (Modulus) It gives the remainder of an integer division. 5 % 2 // 1
++ (Increment) Increases an integer value by one. 3++ or ++3 //4
-- (Decrement) Decreases an integer value by one. 3-- or --3 //2

Example

Example

const add = 3 + 3; //Addition

const sub = 4 - 1; //Subtraction

const a = 3 * 8; // Multiplication

const b = 4/2; //Division

console.log(add, sub, a, b);

Output:

Output

6 3 24 2

Explanation:

  • The + operator is used to add two numbers.
  • The - operator is used to subtract two numbers.
  • The * operator is used to multiply two numbers.
  • The / operator is used to divide two numbers.
  • 2. Assignment Operators

In JavaScript, assignment operators serve the purpose of assigning values to variables. These operators can execute various operations, such as addition or multiplication, prior to the assignment of the resultant value.

Operator Description Example
= It assigns the value of the right operand to the left operand. a = b+ c
+= (Add and) It adds the right operand to the left operand and assigns the result to the left operand. a += b // a = a + b
-= (Subtract and) This operator subtracts the right operand from the left operand and assigns the result to the left operand. a -= b // a = a - b
*= (Multiply and) This operator multiplies the right operand by the left operand and assigns the result to the left operand. a = b // a = a b
/= (Divide and) This operator divides the left operand by the right operand and assigns the result to the left operand. a /= b // a = a / b
% = (Modulus and) This operator takes modulus using two operands and assigns the result to the left operand. a %= b // a= a% b

Example

Example

let a = 20;

a += 2;

a *= 3;

console.log(a);

Output:

Explanation

  • The = operator assigns a value to a variable.
  • The += operator adds and assigns the result to the variable.
  • The *= multiplies and assigns the result to the variable.
  • 3. Comparison Operators

Comparison Operators serve the purpose of evaluating two values and yielding a Boolean result (either true or false). These operators are instrumental in facilitating decision-making processes within conditional Statements.

Operator Description Example
== (Equal to) This operator checks if the value of two operands is equal or not. 2 == 2 gives us true.
!= (Not equal to) This operator is used to check the inequality of two operands. 2 != 3 gives us true.
> (Greater than) It is used to check if the value of the left side is greater than the right side. 3 > 4 gives us false.
< (Less than) It is used to check if the value of the right side is greater than the left side. 4 < 5 gives us true.
>= (Greater than or equal to) This operator checks if the value of the left operand is greater than or equal to the value of the right operand. 3 >= 3 gives us true.
<= (Less than or equal to) This operator checks if the value of the right operand is greater than or equal to the value of the left operand. 3 <= 2 gives us false.
=== (Strictly equal to) This operator checks whether the value and data type of the variable are equal or not. 2 === "2" gives us false.
!== (Strictly not equal to) This operator is used to compare the inequality of two operands and types. 2 === "2" gives us true.

Example

Example

let a = 3;

let b = 5;

let result = a < b;

console.log(result);

Output:

Explanation

It compares the value of a and b using the less-than operator <. Since 3 is less than 5, the result will be true.

4. Logical Operators

In JavaScript, logical operators are used to perform the logical operations that determine the equality or difference between the values.

Operator Description Example
&& This operator checks if both the operands are non-zero; then the condition becomes true. (a && b)
|| This operator checks if any of the operands are non-zero; then the condition becomes true. (a OR b)
! This operator reverses the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false. !expression

Example

Example

const p = true, q = false;

console.log(p && q);

console.log(p || q);

Output:

Output

false

true

Explanation

  • The && operator returns true if both operands are true.
  • The !! Operator returns true if at least one operand is true.
  • 5. Bitwise Operators

In JavaScript, bitwise operators are used to perform operations on binary representative of numbers. In other words, the bitwise operator performs the operations by converting the integers into binary form.

  • & performs a bitwise AND.
  • | performs a bitwise OR.
  • ^ performs a bitwise XOR.
  • ~ performs a bitwise NOT.
Operator Description Example
& The & operator performs a Boolean AND operation on each bit of its integer argument. 5 & 3 = 1
| It compares the corresponding bits of two operands. If either bit is 1, the result bit will be 1; otherwise, it's 0. (5 OR 3) = 7
^ It returns 1 if the bits are different and 0 if they are the same. 5 ^ 3 = 6
~ It inverts all the bits of its operand. It changes each 0 to 1 and each 1 to 0. ~5 = -6
<< It shifts the bits of a number to the left by a specified number of positions. 5 << 1 = 10
>> It moves the bits of a number to the right by a specified number of positions. -10 >> 1 = -5
>>> It shifts the bits of a number to the right by a specified number of positions and fills the vacated bits on the left with zeros. -10 >>> 1 = 2147483643

Example

Example

console.log(3 & 1);

Output:

Explanation

When we transform the resulting string "001" into a decimal number, the output will be "1". Consequently, the expression "1" & "3" yields 1.

6. Ternary Operator

In JavaScript, the ternary operator consists of three operands and is commonly referred to as a conditional operator.

Example

Example

const age = 10;

const status= age >= 18? "Adult": "Minor";

console.log(status);

Output:

Explanation

condition: expression1 ? expression2 evaluates to expression1 if it is true. If expression1 is false, it evaluates to expression2 instead.

7. Delete Operator

In JavaScript, the delete operator serves the purpose of removing a property from an object. This operator not only eliminates the value associated with the property but also the property itself. Once a property has been removed, it cannot be accessed again unless it is reintroduced.

The delete operator is applicable solely to the properties of objects; it cannot be utilized on variables or functions.

Example

Example

const student = {

    rollNum: 27,

    Name: "James Bond",

    Grade: "A",

    Age:19

};

delete student.Age;

console.log(student);

Output:

Output

{ rollNum: 27, Name: 'James Bond', Grade: 'A' }

8. String Operator

In JavaScript, the operators used for strings allow for the merging of two separate strings into a unified string. These string operators function as binary operators, requiring the + symbol to be placed between the two string operands to successfully concatenate them into one cohesive string.

Example

Example

str1 = "Example";

str2 = "Tech";

result = str1 + str2;

console.log(result);

Output:

Output

Example

9. Typeof Operator

In JavaScript, the typeof operator serves as a tool for verifying data types and provides the type of the operand supplied to it. The operand can consist of any variable, function, or object whose type you wish to ascertain through the use of the typeof operator.

Example

Example

console.log(typeof "ExampleTech");

console.log(typeof 50);

console.log(typeof false);

console.log(typeof {});

console.log(typeof undefined);

Output:

Output

string

number

boolean

object

undefined

10. Instanceof Operator

The instanceof operator verifies whether a particular object is an instance of a designated class. When the condition is satisfied, it returns true; otherwise, it yields false.

Example

Example

const n = [1, 2, 23, 4];

console.log(n instanceof Array);

console.log(n instanceof Object);

console.log(n instanceof Number);

console.log(n instanceof String);

Output:

Output

true

true

false

false

11. Chaining Operator

In JavaScript, the optional chaining operator provides a means to safely access properties that are nested multiple levels deep, preventing errors from occurring when a property is not present.

Example

Example

const obj = {name: "Rohit", address: {city: "Noida"}};

console.log(obj.address?.city);

console.log(obj.contact?. phone);

Output:

Output

Noida

undefined

12. Comma Operator

In JavaScript, the comma operator is utilized to sequentially evaluate its operands from the left to the right, ultimately yielding the value of the operand located furthest to the right.

Example

Example

let a, b;

const result = (a = 10, b = 2, a + b);

console.log(result);

Output:

Advance Operator Usage

In JavaScript, there exist advanced applications of operators that you may want to investigate further. These include:

Operator Hierarchy: In JavaScript, it is essential to grasp the sequence in which operators are prioritized in an expression. You have the option to utilize parentheses to dictate the evaluation order when necessary.

Type Coercion: In JavaScript, there are instances where values are automatically converted from one type to another during various operations. This implicit conversion can occasionally result in outcomes that are not anticipated.

Bitwise Operators for Advanced Manipulation: In JavaScript, bitwise operators serve as potent tools for performing intricate bit manipulation operations.

Conclusion

To summarize, operators in JavaScript play a crucial role in executing calculations, enabling data manipulation, and managing the program's flow. They serve as the essential building blocks for all JavaScript applications.

Input Required

This code uses input(). Please provide values below: