Javascript Operators

Abhishek Sharma
By -
0

 JavaScript operators are symbols that perform operations on values and variables. They are fundamental to the language and include a variety of types such as arithmetic, comparison, logical, assignment, and more.


 1. Arithmetic Operators


Arithmetic operators perform basic mathematical operations.


Addition (`+`): Adds two numbers.

  

  let sum = 5 + 3; // 8

  


Subtraction (`-`): Subtracts one number from another.

  

  let difference = 10 - 6; // 4

  


Multiplication (`*`): Multiplies two numbers.

  

  let product = 4 * 7; // 28

  


Division (`/`): Divides one number by another.

  

  let quotient = 20 / 4; // 5

  


Modulus (`%`): Returns the remainder of a division.

  

  let remainder = 10 % 3; // 1

  


Exponentiation (`**`): Raises a number to the power of another number.

 

  let power = 2 ** 3; // 8

  


Increment (`++`): Increases a number by one.

 

  let counter = 0;

  counter++; // 1

  


Decrement (`--`): Decreases a number by one.

  

  let counter = 5;

  counter--; // 4

  


 2. Comparison Operators


Comparison operators compare two values and return a Boolean result.


Equal (`==`): Checks if values are equal.

  

  let isEqual = (5 == '5'); // true

  

Strict Equal (`===`): Checks if values and types are equal.

  

  let isStrictEqual = (5 === '5'); // false

  


Not Equal (`!=`): Checks if values are not equal.

  

  let isNotEqual = (5 != '5'); // false

  


Strict Not Equal (`!==`): Checks if values and types are not equal.

  

  let isStrictNotEqual = (5 !== '5'); // true

  


Greater Than (`>`): Checks if the left value is greater than the right value.

  

  let isGreater = (10 > 5); // true

  


Less Than (`<`): Checks if the left value is less than the right value.

  

  let isLess = (3 < 7); // true

  


Greater Than or Equal (`>=`): Checks if the left value is greater than or equal to the right value.

  

  let isGreaterOrEqual = (5 >= 5); // true

  


Less Than or Equal (`<=`): Checks if the left value is less than or equal to the right value.

  

  let isLessOrEqual = (8 <= 10); // true

  


 3. Logical Operators


Logical operators are used to combine multiple Boolean expressions.


AND (`&&`): Returns true if both operands are true.

  

  let andResult = (true && false); // false

  


-OR (`||`): Returns true if at least one operand is true.

  

  let orResult = (true || false); // true

  


NOT (`!`): Returns true if the operand is false, and vice versa.

  

  let notResult = !true; // false

  

4. Assignment Operators


Assignment operators assign values to variables.


Assignment (`=`): Assigns the value of the right operand to the left operand.

  

  let x = 10;

  


Add and Assign (`+=`): Adds the right operand to the left operand and assigns the result to the left operand.

 

  let a = 5;

  a += 3; // a = 8

  


Subtract and Assign (`-=`): Subtracts the right operand from the left operand and assigns the result to the left operand.

  

  let b = 10;

  b -= 2; // b = 8

  


Multiply and Assign (`*=`): Multiplies the left operand by the right operand and assigns the result to the left operand.

  

  let c = 4;

  c *= 3; // c = 12

  


Divide and Assign (`/=`): Divides the left operand by the right operand and assigns the result to the left operand.

  

  let d = 20;

  d /= 4; // d = 5

  


Modulus and Assign (`%=`): Takes the modulus using the left and right operands and assigns the result to the left operand.

  

  let e = 7;

  e %= 2; // e = 1

  


Exponentiation and Assign (`**=`): Raises the left operand to the power of the right operand and assigns the result to the left operand.

  

  let f = 3;

  f **= 2; // f = 9

  


5. Conditional (Ternary) Operator


The conditional operator assigns a value to a variable based on a condition.



let age = 18;

let canVote = (age >= 18) ? 'Yes' : 'No'; // Yes



 6. Type Operators


Type operators are used to determine the type of a variable.


typeof: Returns the type of a variable.

 

  let dataType = typeof 42; // 'number'

  


instanceof: Checks if an object is an instance of a specific class or constructor.

  

  let date = new Date();

  let isInstanceOf = date instanceof Date; // true

  


 7. Bitwise Operators


Bitwise operators perform operations on binary representations of numbers.


AND (`&`): Performs a bitwise AND.

  

  let bitwiseAnd = 5 & 1; // 1 (0101 & 0001)

  


OR (`|`): Performs a bitwise OR.

  

  let bitwiseOr = 5 | 1; // 5 (0101 | 0001)

  


XOR (`^`): Performs a bitwise XOR.

 

  let bitwiseXor = 5 ^ 1; // 4 (0101 ^ 0001)

  


NOT (`~`): Performs a bitwise NOT.

  

  let bitwiseNot = ~5; // -6 (NOT 0101)

  


-Left Shift (`<<`): Shifts bits to the left.

 

  let leftShift = 5 << 1; // 10 (0101 << 1)

 


Right Shift (`>>`): Shifts bits to the right.


  let rightShift = 5 >> 1; // 2 (0101 >> 1)

  


Zero-fill Right Shift (`>>>`): Shifts bits to the right, filling with zeros.

  

  let zeroFillRightShift = 5 >>> 1; // 2 (0101 >>> 1)

  


 8 Other Operators.


Comma Operator (`,`): Evaluates multiple expressions and returns the value of the last one.

  

  let result = (1 + 2, 3 + 4); // 7

 


Delete Operator: Deletes a property from an object.

  

  let obj = { name: "Alice" };

  delete obj.name; // obj is now {}

  


Void Operator: Evaluates an expression and returns `undefined`.

  

  void function() { console.log("Hello"); }(); // undefined

  


In Operator: Checks if a property exists in an object.

  

  let person = { name: "Alice" };

  let hasName = "name" in person; // true

  


Understanding these operators is essential for writing effective and efficient JavaScript code. Each operator serves a specific purpose and can be combined to perform complex operations.

Tags

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!