JavaScript is more than just variables, loops, and functions. To write efficient, scalable, and professional applications, you must understand some advanced concepts that power modern JavaScript frameworks, libraries, and backend systems. This guide introduces the most important advanced JavaScript concepts in a simple and practical way.
let globalVar = "I am global";
function test() {
let localVar = "I am local";
console.log(globalVar);
}
function counter() {
let count = 0;
return function () {
count++;
return count;
};
}
const increment = counter();
increment(); // 1
increment(); // 2
console.log(a); // undefined
var a = 10;
console.log(b); // ❌ Error
let b = 5;
const user = {
name: "Alex",
greet() {
console.log(this.name);
}
};
user.greet(); // Alex
const show = () => {
console.log(this); // inherits from parent scope
};
A callback function is a function passed as an argument to another function.
function greet(name, callback) {
callback();
console.log("Hello " + name);
}
greet("John", () => {
console.log("Welcome!");
});
const fetchData = new Promise((resolve, reject) => {
let success = true;
if (success) {
resolve("Data received");
} else {
reject("Error occurred");
}
});
fetchData.then(result => {
console.log(result);
}).catch(error => {
console.log(error);
});
async function getData() {
try {
let response = await fetch("https://api.example.com");
let data = await response.json();
console.log(data);
} catch (error) {
console.log(error);
}
}
console.log("Start");
setTimeout(() => {
console.log("Timeout");
}, 0);
console.log("End");
Start
End
Timeout
// math.js
export function add(a, b) {
return a + b;
}
// main.js
import { add } from "./math.js";
console.log(add(2, 3));
const user = { name: "Sam", age: 25 };
const { name, age } = user;
const arr = [10, 20];
const [a, b] = arr;
const nums = [1, 2, 3];
const newNums = [...nums, 4];
function sum(...numbers) {
return numbers.reduce((a, b) => a + b);
}
user?.profile?.email