Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Operators in JavaScript


Operators in JavaScript allow developers to perform various operations on data, including mathematical calculations, value assignments, comparisons, and logical operations. Understanding these operators is essential for effective coding. In this article, we will cover four primary types of operators in JavaScript: arithmetic, assignment, comparison, and logical operators.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numbers. These include addition, subtraction, multiplication, division, and more.

Example: Using arithmetic operators

      let a = 10;
      let b = 5;

      console.log(a + b);  // Addition: Output: 15
      console.log(a - b);  // Subtraction: Output: 5
      console.log(a * b);  // Multiplication: Output: 50
      console.log(a / b);  // Division: Output: 2
      console.log(a % b);  // Modulus (remainder): Output: 0
      console.log(a ** 2); // Exponentiation: Output: 100
    

Arithmetic operators are often used in mathematical calculations, loops, and other scenarios where values need to be computed.

2. Assignment Operators

Assignment operators are used to assign values to variables. The basic assignment operator is =, but there are also compound assignment operators like +=, -=, *=, and more.

Example: Using assignment operators

      let x = 10;
      x += 5;  // Equivalent to x = x + 5
      console.log(x);  // Output: 15

      x -= 2;  // Equivalent to x = x - 2
      console.log(x);  // Output: 13

      x *= 3;  // Equivalent to x = x * 3
      console.log(x);  // Output: 39

      x /= 3;  // Equivalent to x = x / 3
      console.log(x);  // Output: 13

      x %= 4;  // Equivalent to x = x % 4
      console.log(x);  // Output: 1
    

Assignment operators are especially useful for modifying values in a concise way, such as in loops or when updating variables.

3. Comparison Operators

Comparison operators are used to compare two values, returning a Boolean value (true or false) based on the result of the comparison. These are commonly used in conditional statements.

Example: Using comparison operators

      let a = 10;
      let b = 5;

      console.log(a == b);   // Equal to: Output: false
      console.log(a != b);   // Not equal to: Output: true
      console.log(a === 10); // Strict equal to (same type and value): Output: true
      console.log(a !== "10"); // Strict not equal to: Output: true
      console.log(a > b);    // Greater than: Output: true
      console.log(a < b);    // Less than: Output: false
      console.log(a >= 10);  // Greater than or equal to: Output: true
      console.log(a <= b);   // Less than or equal to: Output: false
    

Comparison operators are used to control the flow of the program, such as in if statements or loops, to execute code based on specific conditions.

4. Logical Operators

Logical operators are used to combine multiple conditions, returning a Boolean result based on the combined expression. The three main logical operators are && (AND), || (OR), and ! (NOT).

Example: Using logical operators

      let isAdult = true;
      let hasPermission = false;

      console.log(isAdult && hasPermission);  // AND: Output: false
      console.log(isAdult || hasPermission);  // OR: Output: true
      console.log(!isAdult);  // NOT: Output: false

      let age = 20;
      let isEligible = age >= 18 && isAdult;
      console.log(isEligible);  // Output: true
    

Logical operators are essential in building complex conditions, such as in authentication checks, form validations, and decision-making statements.

Summary of JavaScript Operators

Conclusion

JavaScript provides various operators to perform calculations, assign values, compare data, and control program flow. Mastering these operators is crucial for effective coding in JavaScript, as they form the building blocks of many operations and logic in programming.





Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java