🧮

JavaScript Operators and Conditions

Sep 27, 2025

Overview

This lecture covers JavaScript operators, conditional statements, and how to use them in code to perform calculations and make decisions based on conditions.

Comments in JavaScript

  • Comments are text in code that is not executed, used to explain code.
  • Single-line comments start with //.
  • Multi-line comments are written between /* and */.
  • Comments help describe code and are written in English for global understanding.

Arithmetic Operators

  • Arithmetic operators perform mathematical operations: + (add), - (subtract), * (multiply), / (divide).
  • % is the modulus operator, returning the remainder after division.
  • ** is the exponentiation operator (e.g., a ** b is a to the power of b).
  • Increment (++) increases a variable by 1, decrement (--) decreases by 1.
  • Prefix (++a) changes value before use; postfix (a++) changes after use.

Assignment Operators

  • = assigns a value to a variable.
  • +=, -=, *=, /=, %= perform the operation and assign the result (e.g., a += 4 is a = a + 4).

Comparison Operators

  • == compares values, != checks for inequality.
  • === and !== compare both value and data type (strict comparison).
  • <, <=, >, >= compare numerical values.

Logical Operators

  • && (AND): returns true only if both conditions are true.
  • || (OR): returns true if at least one condition is true.
  • ! (NOT): inverts the truth value.

Conditional Statements

  • if checks a condition and executes code if true.
  • else runs code if the if condition is false.
  • else if checks additional conditions if previous if or else if are false.
  • Conditional statements are used to control code flow based on logic.

Ternary Operator

  • The ternary operator (condition ? expr1 : expr2) returns expr1 if the condition is true, else expr2.
  • Useful for simple if-else decisions in a compact form.

Switch Statement

  • The switch statement selects code to execute based on the value of an expression.
  • Each case represents a possible value; break exits after a match; default runs if no match.

User Input and Practice Questions

  • alert() displays a message to the user.
  • prompt() asks the user for input and returns the value.
  • Practice: Use prompt() to get a number and check if it is a multiple of 5 or 3 (using %).
  • Grading program: Use prompt() and if-else to assign grades based on score ranges.

Key Terms & Definitions

  • Comment — non-executed text in code for documentation.
  • Operator — a symbol that performs an action on variables/values.
  • Operand — a value or variable on which an operator acts.
  • Assignment Operator — assigns values (=, +=, etc.).
  • Comparison Operator — compares values (==, ===, !=, etc.).
  • Logical Operator — combines/complements boolean values (&&, ||, !).
  • Conditional Statement — controls code flow based on conditions (if, else, switch).
  • Ternary Operator — shorthand for simple conditional (condition ? value1 : value2).
  • Prompt — function to get user input.
  • Alert — function to display a popup message.

Action Items / Next Steps

  • Practice writing code using arithmetic, comparison, and logical operators.
  • Complete the grading and multiple-of-five practice problems discussed.
  • Read about the switch statement and explore MDN documentation (developer.mozilla.org) for further learning.