After experimenting with JavaScript, let’s learn how to add some logic using conditional statements. Computers only function by logical rules: whether something is true or false determines if an instructions get executed or not. This logical process is expressed in a conditional statement .

With conditional statements, we will be able to write more complicated applications that can do different things for different inputs. For example: show the user a message “Good morning”, “Good afternoon” or “Good evening” depending on the time of the day.

Watch and follow

Here is a quick overview about conditional statements in JavaScript.

https://www.youtube.com/watch?v=IsG4Xd6LlsM

The next Video shows an example with more complicated if statements

<aside> ▶️

Instructions for the video: Watch the first 11 minutes. Afterwards, the tutorial gets into HTML which is not required for the technical assignment. However, you are welcome to learn extra.

</aside>

https://www.youtube.com/watch?v=PgUXiprlg1k

Summary of the common operators:

Operator Meaning Example
=== Equals if( name === 'admin')
!== Not equals if( age !== 21)
< , > Greater than / less than if( age > 19)
<= , >= Greater than or equals / less than or equals if( age > 19)
! Not if( !isRaining)
&& And if( name === 'admin' && password ==='secret')
` `

<aside> 💡

Tip: In JavaScript, make sure to use the triple equals === for comparing instead of the double equals ==

</aside>

Practice

Make sure to write code on your own. This is the best way of learning programming.

  1. Write a small program that declares a variable called grade and prints a message using console.log as follows:

  2. Write a small program that defines two boolean variables: isSunny and isWarm and prints a message using console.log as follows: