Back to JavaScript
2026-03-095 min read

Expressions and operators by category (JavaScript)

Learn Expressions and operators by category (JavaScript) step by step with clear examples and exercises.

Title: Expressions and Operators by Category (JavaScript) - A full guide

Why This Matters

In JavaScript, expressions and operators play a crucial role in writing efficient and effective code. Understanding them is essential for solving complex problems, debugging issues, and acing coding interviews or exams. Real-life scenarios often involve dealing with various types of expressions and operators to manipulate data, perform calculations, and control program flow.

Prerequisites

To fully grasp the concepts discussed in this lesson, you should have a good understanding of JavaScript syntax, variables, functions, and basic data structures such as arrays and objects. Familiarity with common programming constructs like loops and conditional statements is also beneficial.

Importance of Understanding Expressions and Operators

Understanding expressions and operators is crucial for writing clean, readable, and maintainable code. It helps you to:

  • Perform calculations and manipulate data efficiently
  • Control program flow with logical constructs
  • Write compact and expressive code using shorthand techniques like the ternary operator

Core Concept

Expressions

In JavaScript, an expression is a combination of operators, values, and variables that computes a value. Expressions can be used in statements or as function arguments. There are two types of expressions: Primary expressions and Secondary expressions.

Primary Expressions

Primary expressions have the highest precedence among all expressions. They include:

  • Literals (null, true, false, numbers, strings, arrays, objects)
  • Operator keywords (delete, typeof, void)
  • Parentheses () grouping expressions
  • The special keyword this

Secondary Expressions

Secondary expressions are built from primary expressions using operators. They can be further divided into:

  • Unary Operators: These operate on a single operand and include ++, --, +, -, !, ~, typeof, delete.
  • Binary Operators: These require two operands and include arithmetic operators (+, -, *, /, %), comparison operators (==, !=, <, <=, >, >=), logical operators (&&, ||, !), bitwise operators (&, |, ^, <<, >>, >>>), and the assignment operator (=).
  • Ternary Operator: The ternary operator ?: is a shorthand for an if-else statement.

Operators

Operators are used to perform various operations on values or expressions in JavaScript. They can be categorized as:

Arithmetic Operators

Arithmetic operators are used for mathematical calculations. The arithmetic operators are:

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulus, returns the remainder of division)

Comparison Operators

Comparison operators are used to compare two values and return a boolean value:

  • == (Equal)
  • != (Not equal)
  • < (Less than)
  • <= (Less than or equal to)
  • > (Greater than)
  • >= (Greater than or equal to)
  • === (Strictly equal, checks both value and type)
  • !== (Strictly not equal, checks both value and type)

Logical Operators

Logical operators are used for combining conditional statements:

  • && (Logical AND)
  • || (Logical OR)
  • ! (Logical NOT)

Bitwise Operators

Bitwise operators manipulate the binary representation of numbers:

  • & (Binary AND)
  • | (Binary OR)
  • ^ (Binary XOR, exclusive OR)
  • << (Left shift)
  • >> (Right shift)
  • >>> (Zero fill right shift)

Assignment Operator

The assignment operator assigns a value to a variable:

  • = (Simple assignment)
  • += (Addition assignment)
  • -= (Subtraction assignment)
  • *= (Multiplication assignment)
  • /= (Division assignment)
  • %= (Modulus assignment)
  • &= (Bitwise AND assignment)
  • |= (Bitwise OR assignment)
  • ^= (Bitwise XOR assignment)
  • <<= (Left shift assignment)
  • >>= (Right shift assignment)
  • >>>= (Zero fill right shift assignment)

Ternary Operator

The ternary operator ?: is a shorthand for an if-else statement:

condition ? expression1 : expression2;

Worked Example

Let's consider the following example to illustrate the use of expressions and operators in JavaScript:

let a = 5, b = 3, c;
c = (a + b) * 2 - 1; // Using arithmetic operators
console.log(c); // Output: 13

if (a > b) {
console.log('a is greater than b');
} else {
console.log('b is greater than or equal to a');
} // Using the ternary operator
// Output: 'a is greater than b'

Common Mistakes

Misusing Equality Operators

  • Using == instead of === for strict comparison.
  • Comparing objects with == or ===.

Incorrect Use of Bitwise Operators

  • Forgetting to use parentheses for complex bitwise expressions.
  • Misunderstanding the behavior of bitwise operators in combination with logical operators.

Common Mistakes (Continued)

Type Coercion and Equality Operators

Type coercion can lead to unexpected results when using equality operators. For example:

let a = 5;
let b = "5";
console.log(a == b); // Output: true (due to type coercion)
console.log(a === b); // Output: false (strict comparison)

Bitwise Operators and Logical Operators

Bitwise operators have lower precedence than logical operators, which can lead to unintended results when they are used together without proper parentheses:

let a = 5;
let b = 3;
console.log(a & (b || !a)); // Output: 0 (due to operator precedence)
console.log((a & b) || (!a && b)); // Output: 3 (correct result with parentheses)

Practice Questions

  1. Write a JavaScript expression that calculates the sum of two numbers and assigns it to a variable result.
  2. Write a JavaScript expression using the ternary operator to check if a number is even or odd, and store the result in a variable isEven.
  3. Given two variables x and y, write an expression that checks whether x is greater than y or equal to y, and assigns the result to a variable comparisonResult.
  4. Write a JavaScript expression using the bitwise AND operator (&) to check if a number is even, and store the result in a variable isEven.
  5. Write a JavaScript expression using the logical OR operator (||) to find the maximum of two numbers.

FAQ

What is the difference between == and === in JavaScript?

The == operator performs type coercion, while the === operator does not. This means that == will convert operands to the same type before comparison, whereas === checks for both value and type equality.

How can I perform bitwise operations on strings in JavaScript?

In JavaScript, bitwise operators can only be applied to numbers. However, you can convert a string to its ASCII or Unicode representation and then perform bitwise operations on it.

What is the purpose of the delete operator in JavaScript?

The delete operator removes a property from an object or sets the value of a property to undefined. It does not delete variables, only properties of objects.

Why do bitwise operators have lower precedence than logical operators in JavaScript?

Bitwise operators have lower precedence than logical operators because they operate on binary representations of numbers, and their results can be affected by the order in which they are evaluated. To avoid unintended results, it's important to use parentheses when combining bitwise and logical operators in expressions.

Expressions and operators by category (JavaScript) | JavaScript | XQA Learn