Autocodewizard Logo Operators and Expressions - Autocodewizard Ebooks - JavaScript Essentials: From Fundamentals to Advanced Techniques

Chapter 3: Operators and Expressions

Explore JavaScript operators and expressions, including arithmetic, comparison, and logical operators, to build more dynamic code.

In this chapter, we’ll dive into JavaScript operators and expressions, which are essential for performing calculations, making decisions, and building interactive features in your code. Understanding these operators allows you to write more dynamic and flexible JavaScript code.

Arithmetic Operators

Arithmetic operators allow you to perform basic mathematical operations on numbers:

// Arithmetic operators
let a = 10;
let b = 3;
console.log(a + b);  // Addition, outputs: 13
console.log(a - b);  // Subtraction, outputs: 7
console.log(a * b);  // Multiplication, outputs: 30
console.log(a / b);  // Division, outputs: 3.333...
console.log(a % b);  // Modulus, outputs: 1

The modulus operator % returns the remainder of a division, which is useful for tasks like determining if a number is even or odd.

Assignment Operators

Assignment operators are used to assign values to variables. They can also perform operations as part of the assignment:

// Assignment operators
let x = 5;
x += 3;  // Equivalent to x = x + 3, outputs: 8
x -= 2;  // Equivalent to x = x - 2, outputs: 6
x *= 4;  // Equivalent to x = x * 4, outputs: 24
x /= 3;  // Equivalent to x = x / 3, outputs: 8

These operators help simplify calculations by combining assignment and arithmetic operations in a single line of code.

Comparison Operators

Comparison operators compare two values and return a Boolean value (true or false). They are commonly used in conditional statements:

// Comparison operators
let a = 10;
let b = 5;
console.log(a > b);   // Greater than, outputs: true
console.log(a < b);   // Less than, outputs: false
console.log(a >= b);  // Greater than or equal to, outputs: true
console.log(a <= b);  // Less than or equal to, outputs: false
console.log(a === b); // Strict equality, outputs: false
console.log(a !== b); // Strict inequality, outputs: true

The strict equality === and strict inequality !== operators compare both value and type, ensuring a precise match.

Logical Operators

Logical operators are used to combine Boolean values, often in conditional statements:

// Logical operators
let isSunny = true;
let isWarm = false;
console.log(isSunny && isWarm); // AND, outputs: false
console.log(isSunny || isWarm); // OR, outputs: true
console.log(!isSunny);          // NOT, outputs: false

The AND operator && returns true only if both conditions are true, while the OR operator || returns true if at least one condition is true. The NOT operator ! reverses a Boolean value.

Expressions in JavaScript

Expressions are combinations of variables, operators, and values that JavaScript evaluates to produce a result. Here are some examples:

// Examples of expressions
let a = 5;
let b = 10;

let result = a + b;  // Arithmetic expression, outputs: 15
let isEqual = a === b;  // Comparison expression, outputs: false
let complex = (a < b) && (a + b > 10);  // Logical expression, outputs: true

Expressions are the building blocks of JavaScript logic, allowing you to perform calculations, evaluate conditions, and build complex operations.

Using Expressions in Conditional Statements

Expressions are commonly used in conditional statements, enabling the code to perform different actions based on conditions. For example:

let age = 20;

if (age >= 18) {
    console.log("You are an adult.");
} else {
    console.log("You are a minor.");
}

In this example, the expression age >= 18 evaluates to true or false, and the if statement executes different code blocks based on the result.

Summary and Next Steps

In this chapter, we explored the main types of operators in JavaScript—arithmetic, assignment, comparison, and logical operators—and learned how to use expressions. These are fundamental for writing dynamic code that can make decisions and perform calculations. In the next chapter, we’ll dive into conditional statements, using these operators to create more interactive programs.