Operators

Learn about arithmetic, comparison, logical, assignment, and other types of operators in JavaScript used to perform operations on variables and values.

Present State

JavaScript offers a rich set of operators that are essential for building logic, performing calculations, comparisons, and manipulating values. These operators include arithmetic, comparison, logical, assignment, bitwise, and more. They are used in almost every line of code to control the behavior of expressions and conditions.

Code Example

This code snippet demonstrates various operators in JavaScript:

JavaScript Operators

// Arithmetic Operators
let sum = 10 + 5;        // 15
let difference = 10 - 5; // 5
let product = 10 * 5;    // 50
let quotient = 10 / 2;   // 5
let remainder = 10 % 3;  // 1

// Comparison Operators
console.log(5 > 3);      // true
console.log(5 === "5");  // false
console.log(5 == "5");   // true

// Logical Operators
let a = true, b = false;
console.log(a && b);     // false
console.log(a || b);     // true
console.log(!a);         // false

// Assignment Operators
let x = 10;
x += 5;                  // x = 15

// Ternary Operator
let age = 20;
let status = (age >= 18) ? "Adult" : "Minor";
console.log(status);     // "Adult"
          

This code demonstrates:

  • Basic arithmetic and modulus operations
  • Strict vs loose equality comparison
  • Logical AND, OR, and NOT operations
  • Assignment operator with shorthand
  • Conditional logic using ternary operator

Description

JavaScript operators allow manipulation and evaluation of variables and expressions. They include:
  • Arithmetic Operators: +, -, *, /, %, **
  • Comparison Operators: ==, ===, !=, !==, <, >, <=, >=
  • Logical Operators: &&, ||, !
  • Assignment Operators: =, +=, -=, *=, /=
  • Other Operators: typeof, instanceof, in, ternary (?:)
Understanding how and when to use these operators is key to building logical conditions and expressions in JavaScript.

Applications

  • Form Validation: Use logical and comparison operators to enforce rules.
  • UI Logic: Toggle elements or classes using ternary and logical operators.
  • Calculations: Perform arithmetic in games, dashboards, and financial apps.
  • Loops & Conditions: Use comparison and logical operators to control flow.

Multiple Choice Questions

Q1: What is the result of 5 + '5' in JavaScript?

  • A. 10
  • B. "55"
  • C. NaN
  • D. Error

Answer: B. "55"

Q2: Which operator checks both value and type?

  • A. ==
  • B. !=
  • C. ===
  • D. =

Answer: C. ===

Q3: What does the logical OR (||) operator return?

  • A. The first truthy value or the last value if none are truthy
  • B. Always true
  • C. The first falsy value
  • D. A Boolean only

Answer: A. The first truthy value or the last value if none are truthy

Q4: Which of these is a valid use of the ternary operator?

  • A. let x = if (a > b) then a else b
  • B. let x = (a > b) ? a : b
  • C. let x = a > b ? : a : b
  • D. let x = a > b then a else b

Answer: B. let x = (a > b) ? a : b