Async/Await in JavaScript
Async/Await is a modern way to handle asynchronous operations in a cleaner and more readable way.
It is built on top of Promises and makes async code look like synchronous code
#JavaScript#AsyncAwait#AsyncJS#WebDev#Coding#LearnJS#PythonFSD
Promises in JavaScript ?
A Promise is an object that represents the result of an asynchronous operation it may complete now, later, or fail.
- States of a Promise
- Pending -> Initial state (waiting)
- Fulfilled -> Operation successful
- Rejected -> Operation failed
#Coding
Callbacks in JavaScript
A Callback is a function that is passed as an argument to another function and is executed later, after a task is completed.
Why use Callbacks
- To handle asynchronous operations
- To control execution order
- To run code after a specific task finishes
function processUser(name, callback) {
console.log("Processing " name);
callback();
}
function done() {
console.log("Task Completed");
}
processUser("John", done);
- callback is passed as argument
- It runs after main task
-Callback = Pass function -> Execute later
Asynchronous JavaScript
Asynchronous JS allows code to run without blocking execution.
It lets tasks like API calls, timers, and file loading run in the background while other code continues.
Why use Async?
- Faster performance
- Non-blocking execution
- Better user experience
What is DOM in JavaScript?
The DOM (Document Object Model) is a programming interface that represents an HTML page as a tree of objects.
It allows JavaScript to access, update, and manipulate elements dynamically
- Change text
- Add/remove elements
- Handle user interactions
Accessing Elements in JavaScript
JavaScript uses the DOM to access elements using id and class
/by id
const el = document.getElementById("title");
/by class
const items = document.getElementsByClassName("item");
/modern way
const box = document.querySelector(".box");
When do we use this?
- Update UI after API call
- Handle user input (forms)
- Show/hide elements dynamically
- Add real-time changes to page
- Access DOM -> Update UI -> React to Data (API/User)
#JavaScript#DOM#WebDev#Frontend#Coding#LearnJS#PythonFSD
Events in JavaScript
Events are actions that happen in the browser, like click, typing, scrolling, or hovering.
Why do we use Events?
- To make websites interactive
- To respond to user actions
- To create dynamic behavior (like forms, buttons, animations)
#JavaScript#AI