#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Conditional statements

Conditional statements let a program choose between different paths depending on whether something is true. Checking if a user is logged in, showing a discount only above a certain cart total, or handling a form differently for each option in a dropdown all rely on conditionals. JavaScript provides if, else if, else and switch for this, plus the ternary operator for short expressions.

if, else if and else

An if statement runs its block only when the condition in parentheses is truthy. An optional else block runs otherwise. To test several conditions in turn, chain them with else if. JavaScript checks them from top to bottom and runs only the first block whose condition is true, skipping the rest.

function deliveryCharge(cartTotal, isMember) {
  if (cartTotal <= 0) {
    return null;            // nothing to deliver
  } else if (isMember || cartTotal >= 999) {
    return 0;               // free delivery
  } else if (cartTotal >= 499) {
    return 40;
  } else {
    return 70;
  }
}

console.log(deliveryCharge(1200, false)); // 0
console.log(deliveryCharge(600, false));  // 40

Order matters here. If the >= 499 check came first, a cart of 1200 would match it and never reach the free delivery branch.

Truthy and falsy values

A condition does not have to be a boolean. JavaScript converts it, and exactly these values count as false: false, 0, -0, 0n, "" (empty string), null, undefined and NaN. Everything else is truthy, including "0", "false", empty arrays and empty objects. This is convenient for checks like if (username), but it means an input value of 0 is treated as missing.

switch for many fixed values

When you compare one value against a list of specific options, switch can read more clearly than a long chain. It uses strict equality, so "1" does not match case 1.

function statusLabel(code) {
  switch (code) {
    case 'P':
      return 'Pending';
    case 'S':
    case 'D':
      return 'On its way';   // two cases share one result
    case 'X':
      return 'Cancelled';
    default:
      return 'Unknown';
  }
}

In a switch that does not return, each case needs a break. Without it, execution "falls through" into the next case and runs that code too. Sometimes that is intended, as with 'S' and 'D' above, but forgetting break is a common source of strange output.

For simple either-or values, the ternary operator is a compact alternative: const label = stock > 0 ? 'In stock' : 'Sold out';. It is an expression, so it can sit inside template strings or JSX. Keep ternaries short; nesting them several levels deep is harder to read than an if chain.

Classic mistakes

Using a single = inside a condition assigns instead of comparing. if (role = 'admin') sets role and is always true. Use ===.

Loose equality == converts types before comparing, so 0 == '' and null == undefined are both true. Stick with === unless you have a specific reason.

Deeply nested if blocks also become hard to follow. Returning early for invalid cases, as the first branch of deliveryCharge does, keeps the main logic flat.

What to keep

  • Conditions are checked top to bottom; put the most specific first.
  • Know the eight falsy values.
  • Always compare with ===.
  • Add break or return to every switch case unless fall-through is intended.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course