JavaSript Advanced
JavaSript Advanced
Here are full detailed notes on Advanced JavaScript Concepts, expanding on each topic with examples and key
explanations:
🔁 Higher-Order Functions
Definition: Functions that can take other functions as arguments, or return functions as results.
➤ Use Cases:
Abstracting behavior
Reusability
➤ Example:
javascript
function greet(name) {
return `Hello, ${name}!`;
}
function processUserInput(callback) {
const name = "Alice";
console.log(callback(name));
}
➤ Common HOFs:
Array.prototype.map()
Array.prototype.filter()
Array.prototype.reduce()
🔒 Closures
Definition: A closure is a function that remembers its lexical scope, even when the function is executed outside of
that scope.
➤ Key Insight:
➤ Example:
javascript
function outerFunction() {
let counter = 0;
return function innerFunction() {
counter++;
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/6
return counter;
}
}
➤ Uses:
Data privacy
Function factories
Memoization
➤ Prototype Chain:
Every object has an internal link to another object called its prototype ([[Prototype]] or __proto__).
➤ Example:
javascript
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function() {
return `Hi, I'm ${this.name}`;
};
➤ Inheritance:
javascript
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
⏳ Asynchronous JavaScript
➤ Why Use It?
To prevent blocking of the main thread and allow the browser to remain responsive.
➤ Methods:
1. Callbacks
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/6
javascript
setTimeout(() => {
console.log("Executed after 2 seconds");
}, 2000);
2. Promises
javascript
fetch("https://api.example.com")
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
3. Async/Await
javascript
🚀 ES6 Features
➤ Arrow Functions:
javascript
➤ Template Literals:
javascript
➤ Destructuring:
javascript
➤ Classes:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/6
javascript
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
➤ Exporting:
javascript
// file: math.js
export function add(a, b) {
return a + b;
}
➤ Importing:
javascript
// file: main.js
import { add } from './math.js';
console.log(add(2, 3));
➤ Types:
Named exports
Default exports
🛠️ Error Handling
➤ Try/Catch:
javascript
try {
let result = riskyOperation();
} catch (error) {
console.error("An error occurred:", error);
}
➤ Error Object:
➤ Throwing Errors:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/6
javascript
function divide(a, b) {
if (b === 0) throw new Error("Cannot divide by zero");
return a / b;
}
🌐 Web APIs
➤ DOM API:
javascript
document.getElementById("myDiv").innerText = "Hello!";
➤ Fetch API:
javascript
fetch("https://jsonplaceholder.typicode.com/posts")
.then(res => res.json())
.then(data => console.log(data));
javascript
🔄 Event Loop
Definition: Mechanism that handles execution of multiple chunks of code (callbacks, promises, etc.) in a non-
blocking manner.
➤ Example:
javascript
console.log("Start");
setTimeout(() => {
console.log("Timeout");
}, 0);
console.log("End");
➤ Output:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/6
sql
Start
End
Promise
Timeout
✅ Summary Table:
Concept Key Benefit Common Use Case
Prototypal Inheritance Object sharing and memory efficiency Reuse methods across instances
Let me know if you'd like a printable PDF version or visual diagrams of any of these topics!
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/6