Skip to content
Parwaz DigitalMarketing · Web · Courses

Web Development Course · Module 3, lesson 2

Functions, conditions and comparisons

Organise code into reusable functions, make decisions with if and else, and compare values safely with strict equality.

What you will learn

  • Write functions with parameters and return values, including arrow functions
  • Use if, else if and else to run different code in different cases
  • Compare values with === and combine conditions with && and ||

A function is a named block of code you can run many times. function addTax(price) { return price * 1.1; } takes a price as a parameter and returns a new value. Arrow functions are a shorter form: const addTax = (price) => price * 1.1. Good functions do one clear job and have descriptive names, which makes larger programs easier to read and test.

Conditions let code make decisions. An if statement runs a block only when its condition is true, and else if and else handle other cases. For example, if stock is 0, show "Sold out", else if stock is below 5, show "Only a few left", else show the normal price. Conditions often live inside functions so the same logic can be reused.

Always compare with === and !==, which check both value and type. The looser == converts types first, so 0 == "" is true, which causes subtle bugs. Combine conditions with && meaning and, || meaning or, and ! meaning not. Be aware that values such as 0, an empty string, null and undefined count as false inside a condition, which is useful but sometimes surprising.

Practice task

Write a function stockMessage(stock) that returns "Sold out", "Only a few left" or "In stock" depending on the number, and test it in the console with 0, 3 and 20.

Web Development Course

Learn this properly,with a final project.

These pages summarise each lesson. The full course teaches them hands-on. Message us for the current fee, schedule, and format.

Available for new projects — Replies within one business day