🚀 JavaScript Intermediate Concepts

📌 1. Scope & Block

Variables in JS have scope: var is function-scoped, let and const are block-scoped.

function test() {
  if (true) {
    var x = 10; // function scoped
    let y = 20; // block scoped
  }
  console.log(x); // ✅ 10
  console.log(y); // ❌ ReferenceError
}

📌 2. Hoisting

JS hoists declarations (not initializations). var is hoisted as undefined, but let and const are not accessible before declaration.

console.log(a); // undefined
var a = 5;

console.log(b); // ReferenceError
let b = 10;

📌 3. Closures

A closure is when a function remembers its outer variables even after that outer function has finished executing.

function outer() {
  let count = 0;
  return function inner() {
    count++;
    return count;
  }
}

const counter = outer();
console.log(counter()); // 1
console.log(counter()); // 2

📌 4. The 'this' Keyword

The value of this depends on how a function is called. Arrow functions do not bind this, they inherit it from their lexical scope.

const obj = {
  name: 'JavaScript',
  greet() {
    console.log('Hello, ' + this.name);
  },
};
obj.greet(); // Hello, JavaScript

📌 5. Callback Functions

Functions passed as arguments to be executed later.

function greet(name, callback) {
  console.log('Hi ' + name);
  callback();
}

greet('Dev', function() {
  console.log('Welcome!');
});

📌 6. Arrow Functions

Shorthand function syntax with no own this.

const add = (a, b) => a + b;

📌 7. Template Literals

Backtick syntax for multiline and embedded expressions.

const name = 'Dev';
console.log(`Hello, ${name}!`);

📌 8. Destructuring

Unpack values from arrays or properties from objects.

const [x, y] = [1, 2];
const {name} = {name: 'JS'};
📚 What's Next?
The coming up next page explains explains how async tasks work (like real-world waiting), why Promises matter, and how async/await makes code smooth. You'll learn about class in a friendly way, peek into the event loop (where JS does its multitasking), and even interact with the DOM. It's like getting JavaScript superpowers, step by step.
Know more   →