JavaScript is one of the most powerful and widely used programming languages. Whether you're a beginner or an experienced developer, mastering these core concepts will help you write cleaner, more efficient, and scalable code.
- Closures
Closures allow functions to "remember" the scope in which they were created. They are useful for data privacy and function factories.
Example:
function outerFunction(outerVariable) {
return function innerFunction(innerVariable) {
console.log(
Outer: ${outerVariable}, Inner: ${innerVariable}
); }; }
const closureExample = outerFunction("Hello");
closureExample("World"); // Output: Outer: Hello, Inner: World
- Hoisting Hoisting is JavaScript's default behavior of moving variable and function declarations to the top of their scope before execution.
Example:
console.log(hoistedVar); // Undefined (not an error)
var hoistedVar = "I am hoisted";
hoistedFunction(); // Works because function declarations are hoisted
function hoistedFunction() {
console.log("I am hoisted too!");
}
- The Event Loop The event loop enables JavaScript’s asynchronous behavior, allowing non-blocking operations using callbacks, promises, and async/await.
Example:
console.log("Start");
setTimeout(() => {
console.log("Inside setTimeout");
}, 0);
console.log("End");
Top comments (0)