Operators In JavaScript

 In JavaScript, operators are symbols that perform operations on operands, such as variables, values, or expressions. They allow you to manipulate data and perform various calculations. JavaScript supports a wide range of operators, including arithmetic, assignment, comparison, logical, bitwise, and more. Below, I'll provide an overview of the main types of operators in JavaScript along with examples:

1. Arithmetic Operators:

Arithmetic operators are used to perform mathematical calculations.

  • Addition (+): Adds two operands.

  • let sum = 5 + 3; // sum equals 8

  • Subtraction (-): Subtracts the second operand from the first.
  • let difference = 10 - 4; // difference equals 6
  • Multiplication (*): Multiplies two operands.
  • let product = 4 * 6; // product equals 24
  • Division (/): Divides the first operand by the second.
  • let quotient = 20 / 5; // quotient equals 4
  • Modulus (%): Returns the remainder of a division.
  • let remainder = 10 % 3; // remainder equals 1

2. Assignment Operators:

Assignment operators are used to assign values to variables.

  • Assignment (=): Assigns a value to a variable.
  • let x = 5; // variable x is assigned the value 5

3. Comparison Operators:

Comparison operators are used to compare values.

  • Equality (==): Checks if two values are equal.

  • console.log(5 == 5); // true

  • Inequality (!=): Checks if two values are not equal.
  • console.log(5 != 3); // true
  • Greater than (>): Checks if the left operand is greater than the right operand.
  • console.log(8 > 3); // true
  • Less than (<): Checks if the left operand is less than the right operand.
  • console.log(2 < 7); // true
  • Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
  • console.log(5 >= 5); // true
  • Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
  • console.log(3 <= 5); // true

4. Logical Operators:

Logical operators are used to perform logical operations.

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

  • console.log(true && true); // true

  • OR (||): Returns true if at least one operand is true.
  • console.log(true || false); // true
  • NOT (!): Returns the opposite of the operand's logical state.
  • console.log(!true); // false

5. Bitwise Operators:

Bitwise operators are used to perform bitwise operations.

  • AND (&): Performs a bitwise AND operation.
  • OR (|): Performs a bitwise OR operation.
  • XOR (^): Performs a bitwise XOR (exclusive OR) operation.
  • NOT (~): Performs a bitwise NOT operation.
  • Left shift (<<): Shifts the bits of the first operand to the left by the number of positions specified by the second operand.
  • Right shift (>>): Shifts the bits of the first operand to the right by the number of positions specified by the second operand.

These are some of the main types of operators in JavaScript, each serving a specific purpose in performing operations on data.

Post a Comment

0 Comments