From the course: JavaScript Essential Training

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Conditional switch statement

Conditional switch statement - JavaScript Tutorial

From the course: JavaScript Essential Training

Conditional switch statement

- [Instructor] The if else statement is a useful tool, but in many cases it's not the right tool for the job. Let's say you want to output different values depending on how old the backpack is. So if it's under 30 days old, it's new. If it's under one year old, it's lightly used. If it's one year or older, it's used. And if it's more than three years old, it's old. We could do this by nesting conditional statements inside one another. But as you can see from the code example it gets clunky and hard to read. Here we're saying, if the age is over 30, and if the age is over 365, and if the age is over 1095, then it's old. Otherwise it's used, otherwise it's lightly used. Otherwise it's new. This is not only hard to write, it's also hard to read. Instead, when we want to get different results depending on various conditions, we can use a switch statement. Here if we look at the code example, we provide an…

Contents