Search⌘ K
AI Features

Discussion: On or Off?

Understand how to use if else statements and the ternary operator to control code flow based on conditions. Learn to map values using objects to write compact and efficient JavaScript logic.

Verifying the output

Now, it’s time to execute the code and observe the output.

Javascript (babel-node-es2024)
let response = "on",
state = {on: 1, off: 0}[response]
console.log(state);

The trusty ifelse

Suppose we have a light switch that can be turned on or off. We ...