JavaScript Lesson 8: Conditionals

0
15

Introduction

What if conditionals were easy? Well then you’d learn them pretty quick! Welcome to conditionals, one of the most logical parts of programming. If andelse are all keywords used in JavaScript. We also have a bonus keyword called the switch statement which we will talk about towards the end of the lesson. But let’s jump right in.

The if statement

Have you ever wondered what if? Well then you already have a firm understanding of this JavaScript conditional statement. The if statement is used to ask a question with two possible answers. One of the answers being true. Let’s check out the below example before talking further.

What this conditional statement checks is whether the variable sheLovesMe is equal to true. If true, then the variable today is set to the string “Happy Day”. But what if sheLoveMe equals false? Let’s check that out in the next section.

The else statement

The else statement defines what happens when an if statement fails.

For this example, in the instance sheLovesMe is false, the variable today is set to “There is always tomorrow”. Pretty simple right? And yet it is one of the most powerful parts of programming.

The if else statement

The switch statement

Summary

  • if asks a question which results in either true or false
  • else executes if the condition is false
  • switch statements are an effective alternative to if statements

Next Lesson

Next we will be talking about another powerful JavaScript tool. JavaScript Loops allow for you to perform tasks quickly and effectively.

Leave a Reply