• Home
  • Python
    • Introduction to Python
    • Python Developer
    • Expert Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
    • Expert JavaScript Developer
  • React.js
    • Introduction to React
    • React Developer
    • Expert React Developer
  • Linux
    • CyberSecurity - Introduction to Linux
    • CyberSecurity - Linux Administrator
    • CyberSecurity - Expert Linux Administrator
  • Active Directory
    • CyberSecurity - Introduction to Active Directory
    • CyberSecurity - Active Directory Administrator
    • CyberSecurity - Expert Active Directory Administrator
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • Python
      • Introduction to Python
      • Python Developer
      • Expert Python Developer
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
      • Expert JavaScript Developer
    • React
      • Introduction to React
      • React Developer
      • Expert React Developer
    • Linux
      • CyberSecurity - Introduction to Linux
      • CyberSecurity - Linux Administrator
      • CyberSecurity - Expert Linux Administrator
    • Active Directory
      • CyberSecurity - Introduction to Active Directory
      • CyberSecurity - Active Directory Administrator
      • CyberSecurity - Expert Active Directory Administrator
  • Interactive Training
  • Pricing
  • Brainstorm

Quick Links

  • About Us
  • Pricing
  • Brainstorm

Courses

  • Python
    • Introduction to Python
    • Python Developer
    • Expert Python Developer
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
    • Expert JavaScript Developer
  • React
    • Introduction to React
    • React Developer
    • Expert React Developer
    • Professional Master React Developer
  • Linux
    • CyberSecurity - Introduction to Linux
    • CyberSecurity - Linux Administrator
    • CyberSecurity - Expert Linux Administrator
  • Active Directory
    • CyberSecurity - Introduction to Active Directory
    • CyberSecurity - Active Directory Administrator
    • CyberSecurity - Expert Active Directory Administrator

Newsletter

Subscribe to our monthly newsletter, for a quick update on Python, JavaScript, React news

© 2024 - 2026 STEMTrainingGrounds. All Rights Reserved.

Lesson 6

Lesson 6 of 16

Conditions - if

Lesson Progress: 0%

Code Example

let age = 18;

if (age >= 18) {
  console.log("You are an adult.");
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

JavaScript Code Editor

Task Incomplete

Editor Input:

Loading...

Editor Output:

Click "Run Code" to see the output here
  • The if statement checks if something is true.
  • It compares the value of age to see if it is 18 or more.
  • If the condition is true, the message inside the curly braces { } will run.

Code Example

let number = 7;

if (number % 2 === 0) {
  console.log("Even number");
} else {
  console.log("Odd number");
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

JavaScript Code Editor

Task Incomplete

Editor Input:

Loading...

Editor Output:

Click "Run Code" to see the output here
  • The if statement checks if something is true.
  • It tests if the number can be divided by 2 with no leftover.
  • If that is true, it prints "Even number".
  • If it is not true, the else part runs and prints "Odd number".

Code Example

let score = 85;

if (score >= 90) {
  console.log("Grade A");
} else if (score >= 75) {
  console.log("Grade B");
} else if (score >= 50) {
  console.log("Grade C");
} else {
  console.log("Fail");
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

JavaScript Code Editor

Task Incomplete

Editor Input:

Loading...

Editor Output:

Click "Run Code" to see the output here
  • The if statement checks the first condition to see if it is true.
  • If the first condition is not true, the else if checks another condition.
  • It keeps checking each else if until one condition is true.
  • If none of the conditions are true, the else part runs.

Code Example

let username = "admin";
let password = "1234";

if (username === "admin") {
  if (password === "1234") {
    console.log("Login successful");
  } else {
    console.log("Wrong password");
  }
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

JavaScript Code Editor

Task Incomplete

Editor Input:

Loading...

Editor Output:

Click "Run Code" to see the output here
  • The first if checks if the username is correct.
  • Inside it, there is another if that checks the password.
  • This is called a nested if because it is inside another if.
  • If the password is wrong, the else part runs and shows a different message.

Code Example

let age = 25;
let hasID = true;

if (age >= 18 && hasID) {
  console.log("Entry allowed");
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

JavaScript Code Editor

Task Incomplete

Editor Input:

Loading...

Editor Output:

Click "Run Code" to see the output here
  • The if statement checks if something is true before running the code inside it.
  • The && symbol means "and".
  • Both conditions must be true for the code to run.
  • If one condition is false, nothing inside the if block will happen.
Lesson Progress: 0%
Conditions - if
Lesson Incomplete
← Previous: Boolean Operators
Lesson 6 of 16
Next: Conditions - switch →