This stage focuses on the fundamental building blocks of JavaScript.
Topics Covered:
var, let, and const. Understanding the differences between them.if, else if, else, switch) and loops (for, while, do...while, for...in, for...of).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: