const promise = new Promise((resolve, reject) => {
setTimeout(() => resolve("Done!"), 1000);
});
promise.then(data => {
console.log(data); // Done!
});
JavaScript Deep Dive β Post 4
π Promises
Promises handle asynchronous operations. They can be in 3 states: pending, fulfilled, or rejected.
β‘ async / await
A cleaner way to write async code using Promises.
async function fetchData() {
try {
const result = await fetch("https://api.example.com");
const data = await result.json();
console.log(data);
} catch (error) {
console.error("Error:", error);
}
}
fetchData();
π« Classes
JavaScript supports object-oriented programming with
class.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a sound.`);
}
}
const dog = new Animal("Dog");
dog.speak(); // Dog makes a sound.
β³ Event Loop
JavaScript runs in a single-threaded environment using the event loop to manage tasks.
console.log("Start");
setTimeout(() => {
console.log("Timeout done");
}, 0);
console.log("End");
// Output:
// Start
// End
// Timeout done
JS first finishes the main stack, then handles callbacks in the queue!
π§© DOM Interactions
Use JavaScript to manipulate webpage elements dynamically.
// HTML: <button id="clickMe">Click Me</button>
document.getElementById("clickMe").addEventListener("click", () => {
alert("Button clicked!");
});
Try this with real buttons and forms β itβs where magic begins β¨
π Done with Post 4!
Now you're ready to start building apps, games, and interactive sites. You just unlocked serious JS
power! πͺ
Go to Editor
π What's Next?
Congratulations! π You've completed the core JavaScript course β from variables and functions to async
programming and
DOM interactions.
Now itβs time to move beyond syntax and start thinking like a real developer.
π Explore practical examples inside the app
π§ Understand logic step by step
π‘ Learn how to build small features, tools, and logic-based snippets by observing working code
Donβt just memorize β see it, break it, and build it.
Your journey from learning to writing logic starts now.
Write Logic →