05 - JavaScript - Operators

05 - JavaScript - Operators

JavaScript operators are symbols or keywords used to perform various operations on one or more values, producing new values. There are several types of operators in JavaScript. Arithmetic operators perform mathematical operations like addition, subtraction, multiplication, division, and modulus. Comparison operators test if two values are equal or not, while logical operators are used to test if a condition is true or false. Assignment operators store values in variables. Other operators include bitwise, string, and conditional operators, which enable more complex operations. JavaScript operators are useful for performing a range of operations on values, from simple arithmetic to complex logical operations. Whether you need to manipulate binary data, strings or create conditional statements, there is an operator for the job. Overall, JavaScript operators are crucial for web development and programming, making it an essential tool for developers.

Types of JS Operators

Types of JS Operators The different types of JS operators are as follows -

  • Arithmetic Operators

  • Assignment Operators

  • Comparison Operators

  • String Operators

  • Logical Operators

  • Bitwise Operators

  • Ternary Operators

  • Type Operators

Logical Operators

JavaScript logical operators are used to combine two or more expressions and return a boolean value based on the result of the evaluation. There are three logical operators in JavaScript -

  1. Logical AND (&&): The logical AND operator returns true if both expressions are true, and false otherwise. For example, (5 > 3) && (7 < 10) will return true.

  2. Logical OR (||): The logical OR operator returns true if either one of the expressions is true, and false otherwise. For example, (5 > 3) || (7 > 10) will return true.

  3. Logical NOT (!): The logical NOT operator reverses the boolean value of an expression. For example, !(5 > 3) will return false.

Logical operators are often used in conditional statements and loops to control the flow of a program based on certain conditions. For example, if ((x > 5) && (y < 10)) will execute the code within the if statement only if both conditions evaluate to true.

Overall, logical operators in JavaScript are used to combine expressions and control the flow of a program based on certain conditions.

Bitwise Operators

JavaScript bitwise operators are used to manipulate the binary representation of numbers. There are six bitwise operators in JavaScript -

  1. Bitwise AND (&): The bitwise AND operator sets each bit to 1 if both bits are 1. For example, 5 & 3 will return 1 because the binary representation of 5 is 101 and the binary representation of 3 is 011.

  2. Bitwise OR (|): The bitwise OR operator sets each bit to 1 if either bit is 1. For example, 5 | 3 will return 7 because the binary representation of 5 is 101 and the binary representation of 3 is 011.

  3. Bitwise XOR (^): The bitwise XOR operator sets each bit to 1 if only one of the bits is 1. For example, 5 ^ 3 will return 6 because the binary representation of 5 is 101 and the binary representation of 3 is 011.

  4. Bitwise NOT (~): The bitwise NOT operator inverts all the bits of a number. For example, ~5 will return -6 because the binary representation of 5 is 101 and inverting all the bits will result in -110 in binary form which is equal to -6 in decimal form.

  5. Left shift (<<): The left shift operator shifts the binary representation of a number to the left by a specified number of bits. For example, 5 << 1 will return 10 because the binary representation of 5 is 101 and shifting the bits one position to the left will result in 1010.

  6. Right shift (>>): The right shift operator shifts the binary representation of a number to the right by a specified number of bits. For example, 5 >> 1 will return 2 because the binary representation of 5 is 101 and shifting the bits one position to the right will result in 10.

Bitwise operators are used to perform low-level operations in JavaScript, such as manipulating the bits of numbers, working with flags or masks, and optimizing performance in certain algorithms.

Summarizing Up

JavaScript logical operators are used to combine two or more expressions and return a boolean value based on the result of the evaluation. These operators include &&, ||, and !, and are often used in conditional statements and loops to control the flow of a program based on certain conditions.

JavaScript bitwise operators are used to manipulate the binary representation of numbers. These operators include &, |, ^, ~, <<, and >>, and are used to perform low-level operations, such as manipulating the bits of numbers, working with flags or masks, and optimizing performance in certain algorithms.

Both logical and bitwise operators are important in JavaScript programming and are used in different contexts to perform various tasks.

Did you find this article valuable?

Support manas krishna jaiswal by becoming a sponsor. Any amount is appreciated!