Skip to content
My Compiler
LoginRegister

Learn JavaScript: a Beginner Tutorial

Follow these lessons in order. Each one has a short explanation and code you can run instantly in My Compiler.

  1. Lesson 1: Hello, World!

    Every language starts here: print a message to the screen to check that everything works.

    console.log("Hello, World!");
    Open in compiler
  2. Lesson 2: Variables

    Variables store values such as text, whole numbers and decimals so you can reuse them.

    const name = "Ada";
    let age = 21;
    const height = 1.75;
    console.log(name, age, height);
    Open in compiler
  3. Lesson 3: Conditions

    if / else lets your program choose a path depending on a value.

    const age = 18;
    if (age >= 18) {
      console.log("Adult");
    } else if (age >= 13) {
      console.log("Teenager");
    } else {
      console.log("Child");
    }
    Open in compiler
  4. Lesson 4: Loops

    Loops repeat a block of code: use for when you know the count and while when you wait for a condition.

    for (let i = 1; i <= 5; i++) {
      console.log(i);
    }
    
    let n = 3;
    while (n > 0) {
      console.log("Countdown", n--);
    }
    Open in compiler
  5. Lesson 5: Functions

    A function packs code into a reusable block that takes inputs and returns a result.

    function add(a, b) {
      return a + b;
    }
    
    console.log(add(2, 3));
    Open in compiler
  6. Lesson 6: Fibonacci sequence

    Each number is the sum of the two before it. A classic exercise that combines variables, loops and arithmetic.

    function fib(n) {
      let a = 0, b = 1;
      const out = [];
      for (let i = 0; i < n; i++) {
        out.push(a);
        [a, b] = [b, a + b];
      }
      console.log(out.join(" "));
    }
    
    fib(10);
    Open in compiler