Category: Examples

  • If-Else Condition

    let marks = 75;
    
    if (marks >= 50) {
      console.log("You passed!");
    } else {
      console.log("You failed!");
    }
    

    Explanation:

    • If condition checks if marks >= 50.
    • If true → “You passed!”, otherwise → “You failed!”.
  • Simple Function

    function greet(user) {
      return "Hello, " + user + "!";
    }
    
    console.log(greet("Ali"));
    

    Explanation:

    • Functions allow code reusability.
    • greet("Ali") returns "Hello, Ali!".
  • Variables and Data Types

    let name = "Ali";      // String
    let age = 20;          // Number
    let isStudent = true;  // Boolean
    
    console.log(name, age, isStudent);
    

    Explanation:

    • let and const are used for declaring variables.
    • JavaScript automatically detects data types (string, number, boolean, etc.).
  • Print a Message

    console.log("Hello, JavaScript!");
    

    Explanation:

    • console.log() is used to print messages to the browser console.
    • Useful for debugging and testing code.