Learning JavaScript can feel like learning superpowers! π¦ΈββοΈ But sometimes, those powers act a little weird and confuse us. Donβt worry, in this blog, weβll break down the tricky parts step-by-step, like solving a fun puzzle together. Ready? Letβs go! ‡
π What We'll Learn
- Scope
- Hoisting
- Different Kinds of Functions
- Promises
- Fetch API
- Promise.all
- Async/Await
Note: These topics are often confusing, but once you get them, you'll feel like a JavaScript superhero π¦ΈββοΈ!
1. Scope π
Scope means: Where can I use this variable?
let toy = 'Teddy'; // Global scope function play() { let game = 'Hide and Seek'; // Function scope if (true) { let snack = 'Chips'; // Block scope console.log(toy, game, snack); } // console.log(snack); // β Error: snack is not visible here }
-
Global Scope
= Everyone can see it π -
Function Scope
= Only inside the function πͺ -
Block Scope
= Only inside{}
likeif
,for
blocks π
2. Hoisting ποΈ
Hoisting means: JavaScript lifts your variable or function declarations to the top!
console.log(x); // undefined (hoisted) var x = 5; // console.log(y); // β Error let y = 10;
-
var
is hoisted and becomesundefined
-
let
andconst
are also hoisted, but you can't use them before you write them (they live in the Temporal Dead Zone) β οΈ
3. Types of Functions π§
Function Declaration
function greet() { console.log('Hello!'); }
β Hoisted. You can call it before it's written.
Function Expression
const sayHi = function() { console.log('Hi!'); };
β Not hoisted. You must write it first.
Arrow Function
const sayHey = () => console.log('Hey!');
β‘οΈ Shorter. No own this
.
IIFE (Immediately Invoked Function Expression)
(function() { console.log('I run right away!'); })();
β‘ Used when you want to run code immediately.
4. Promises π
A Promise is like a gift. You wait... then you get something or nothing!
const surprise = new Promise((resolve, reject) => { let isGood = true; if (isGood) { resolve('π Yay!'); } else { reject('π’ Oops!'); } }); surprise .then(msg => console.log(msg)) .catch(err => console.log(err));
β
.then()
is for good result
β .catch()
is for error
5. Fetch API π
Fetch is how JavaScript gets things from the internet - like food delivery π
fetch('https://jsonplaceholder.typicode.com/posts/1') .then(response => response.json()) .then(data => console.log(data.title)) .catch(error => console.error('Error:', error));
6. Promise.all π§Ί
Promise.all waits for many promises at once, like waiting for all friends to arrive before cutting the cake π
const p1 = Promise.resolve('π'); const p2 = Promise.resolve('π'); const p3 = Promise.resolve('π'); Promise.all([p1, p2, p3]) .then(result => console.log(result)) // ["π", "π", "π"] .catch(error => console.log('One failed:', error));
π§ All promises must finish. If even one fails, you go to .catch()
.
7. Async/Await π§
Async/Await helps you write promises in a clean way, like reading a book π
async function getPost() { try { const response = await fetch('https://jsonplaceholder.typicode.com/posts/1'); const post = await response.json(); console.log(post.title); } catch (err) { console.log('Error:', err); } } getPost();
-
async
means the function will useawait
-
await
waits for the promise
π Quick Review Table
Concept | Key Idea |
---|---|
Scope | Where a variable is available |
Hoisting | Declarations move to the top |
Functions | Different ways to write functions |
Promises | Handling future events (success/fail) |
Fetch | Get data from a server |
Promise.all | Wait for multiple promises |
Async/Await | Cleaner syntax for promises |
JavaScript is fast, flexible, and fun and now, you're learning its coolest tricks!
Top comments (0)