Lesson 4 of 15

Conditionals

Conditionals

if/else lets your program take different paths based on a condition:

if (temperature > 30) {
    console.log("hot");
} else if (temperature > 15) {
    console.log("warm");
} else {
    console.log("cold");
}

Comparison Operators

x === y   // strictly equal (same type and value)
x !== y   // strictly not equal
x > y     // greater than
x < y     // less than
x >= y    // greater than or equal
x <= y    // less than or equal

Always use === (triple equals) instead of == (double equals) in JavaScript. == does type coercion and produces surprising results.

Logical Operators

a && b    // true if both are true
a || b    // true if either is true
!a        // true if a is false

Your Task

Write function classify(n) that returns:

  • "positive" if n > 0
  • "negative" if n < 0
  • "zero" if n === 0
JavaScript loading...
Loading...
Click "Run" to execute your code.