Stage 1: JavaScript Basics - Laying the Foundation

This stage focuses on the fundamental building blocks of JavaScript.

Topics Covered:

Explanations and Code Examples:

JavaScript

// Variables
let age = 30;
const name = "Alice";
var oldVariable = "This is generally discouraged now";

// Data Types
let count = 10; // Number
let message = "Hello"; // String
let isTrue = true; // Boolean
let nothing = null; // Null
let notDefined; // Undefined
let uniqueId = Symbol('id'); // Symbol
let bigNumber = 9007199254740991n; // BigIntlet person = { firstName: "Bob", lastName: "Doe" }; // Object

// Operators
let sum = 5 + 3;
let isEqual = (age === 30);
let isAdult = age >= 18 && hasLicense;

// Control Flow
if (age >= 18) {
  console.log("Adult");
} else {
  console.log("Minor");
}

for (let i = 0; i < 5; i++) {
  console.log(i);
}

// Functions
function greet(personName) {
  return `Hello, ${personName}!`;
}
console.log(greet(name));

// Scope
let globalVar = "I am global";
function checkScope() {
  let localVar = "I am local";
  console.log(globalVar); // Accessible
  console.log(localVar); // Accessible here
}
checkScope();
// console.log(localVar); // Error: localVar is not defined outside checkScope

// Closures
function outerFunction(outerVar) {
  return function innerFunction(innerVar) {
    console.log(outerVar);
    console.log(innerVar);
  };
}
const myInnerFunction = outerFunction("Hello from outer");
myInnerFunction("Hello from inner"); // Output: Hello from outer, Hello from inner

Practice Exercises:

  1. Declare variables of different data types and perform basic operations on them.
  2. Write conditional statements to check different scenarios (e.g., if a number is positive, negative, or zero).
  3. Create loops to iterate through a range of numbers or characters.
  4. Define functions that perform specific tasks and return values.
  5. Experiment with variable scope by declaring variables in different parts of your code.
  6. Write a function that creates and returns an inner function (a closure) that remembers a variable from the outer function.