var number = 15;

switch(number) {

default:

console.log(‘No match found’);

break;

case 5:

case 10:

console.log(‘number is 5 or 10’);

break;

case 15:

console.log(‘number is 15’);

}

As you can see, we have ‘chained’ together case 5 and case 10, which means that if number is equal to either 5 or to 10, then the same code will run. This helps to keep our code nice and clean, which makes it easier to interpret and debug, which, as we’ve established, is incredibly important.

So that’s conditional statements for you. It is a powerful, core part of JavaScript, and all programming languages for that matter. It can’t be understated how much of a huge part of JavaScript conditional statements make up. You will struggle to find any JavaScript code that doesn’t contain at least one conditional statement. They are so useful and actually quite easy to write and work with. Now that you understand them, you are so close to being able to claim that you understand all of the core aspects of JavaScript. We have one small section left, before we can put everything we have learned into practice as we go off and create our first dynamic website. Lastly for JavaScript, let’s look at events.

Events

It’s important to remember that JavaScript’s main purpose is to integrate with the browser and your website. JavaScript’s tight integration with the browser is one of the main features of JavaScript. The benefit of using JavaScript with your website is that we are able to write JavaScript code that can ‘react’ to certain ‘events’ that occur on your website.

What’s an event? An event is simply something that a browser or a user does. Some examples of events are when a website has finished loading, when a button is clicked, when a user hovers over a div, when a form is submitted and so on. Using JavaScript, we can react to these events by executing code snippets that we define in advance.

We can ‘tie’ these actions to the events in our HTML. We can simply assign the action directly to the element we want to react to, like so: