JavaScript Basics Tutorial

An overview of Javascript fundementals

Conditional Statements

Conditional statements are used to perform different actions based on different conditions. Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In JavaScript we have the following conditional statements:

Example


if (greeting === "hello" && question === "What's the time?") {
    //this block of code is executed if the 1st condition is true
    console.log("It is 10 o'clock");   
}
else if (greeting === "Hello" || question == "What is the weather like tomorrow?") {
    //this block of code is executed if the 1st condition is false and the 2nd condtion is true
    console.log("It will be suuny tomorrow.");
}
else {
    //this block of code is executed if the 1st and 2nd condition are false
    console.log("I don't understand the question.")
}

            

Comparison and Logical operators are used to test for true or false.

Comparison operators

  • == equal to
  • === equal value and equal type
  • != not equal
  • !== not equal value or not equal type
  • > greater than
  • < less than
  • >= greater than or equal to
  • <= less than or equal to

Logical operators

  • && and
  • || or
  • ! not

Exercise

Complete the statement to print "Hello World!" if num1 is greater than num 2.


let num1 = 20;
let num2 = 15;

if (    ) {
    console.log("Hello World!")
}

  try Again!
            
 prev  Variables
Arrays & Loops  next