From the course: Learning CoffeeScript

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Using the switch statement

Using the switch statement - CoffeeScript Tutorial

From the course: Learning CoffeeScript

Start my 1-month free trial

Using the switch statement

Switch statements are one of those things that I don't use really much in JavaScript. The syntax is extremely messy. I never like how you had to add a break statement at the end of every switch. It just looked really messy. Now, the switch statement is designed to allow you to execute commands depending on the value of an element. So, let's see how that works in how you use em in Coffee Script. So, let's go ahead and add a function here called append. And it's going to take a value called item. So, this is just going to go to the content and just append whatever we pass into it. So, I'll use string interpolation here and a pass along item. Then I'm going to create here an author and set it to a value. So, I'll just create an author called Bob here, set it to an author. And then I'm going to create a switch with the coffee script version of the statement. So, all you do is you say, switch and then you put in what you want to test. And you pretty much use English like commands. You say,…

Contents