JavaScript Core Concepts & Mind-Blowing Tricks
π JavaScript Core Concepts & Mind-Blowing Tricks Every Developer Must Know! π‘π₯
JavaScript is not just a languageβitβs a powerhouse of dynamic behavior, flexibility, and hidden magic. Whether youβre building web apps, APIs, or working with frameworks like React, mastering core concepts is the key to becoming a pro developer πͺ
Letβs dive deep into JavaScript fundamentals + surprising tricks that will level up your coding game π
π§ 1. Execution Context & Call Stack
JavaScript runs code inside an Execution Context.
πΉ Types:
- Global Execution Context
- Function Execution Context
πΉ Example:
function greet() {
console.log("Hello");
}
greet();
π Behind the scenes:
- Global context created
greet()pushed to Call Stack- Executes β then removed
π‘ Concept Tip: JavaScript is single-threaded, but uses the call stack to manage execution.
π 2. Hoisting (Magic Before Execution)
Variables and functions are moved to the top during compilation.
πΉ Example:
console.log(a); // undefined
var a = 10;
π Internally:
var a;
console.log(a);
a = 10;
β οΈ But with let & const:
console.log(b); // β ReferenceError
let b = 20;
π‘ This is called the Temporal Dead Zone (TDZ)
π 3. Closures (Superpower π₯)
A closure is when a function remembers its outer scope, even after execution.
πΉ Example:
function outer() {
let count = 0;
return function inner() {
count++;
console.log(count);
};
}
const counter = outer();
counter(); // 1
counter(); // 2
π‘ Used in:
- Data privacy
- Memoization
- Event handlers
π― 4. Scope & Lexical Environment
Scope defines where variables are accessible
πΉ Types:
- Global Scope
- Function Scope
- Block Scope (
let,const)
πΉ Example:
function test() {
let x = 10;
}
console.log(x); // β Error
π‘ JavaScript uses Lexical Scoping β scope depends on where variables are written.
β‘ 5. Event Loop (Async Power π₯)
JavaScript handles async tasks using:
- Call Stack
- Web APIs
- Callback Queue
- Event Loop
πΉ Example:
console.log("Start");
setTimeout(() => {
console.log("Async Task");
}, 0);
console.log("End");
π Output:
Start
End
Async Task
π‘ Even 0ms delay goes through the event loop
π 6. Promises & Async/Await
Modern async handling π
πΉ Promise Example:
const promise = new Promise((resolve, reject) => {
resolve("Success!");
});
promise.then(res => console.log(res));
πΉ Async/Await:
async function fetchData() {
const res = await Promise.resolve("Data loaded");
console.log(res);
}
fetchData();
π‘ Cleaner & readable async code
π 7. This Keyword (Tricky π΅)
this depends on how a function is called
πΉ Example:
const obj = {
name: "Lakhveer",
greet() {
console.log(this.name);
}
};
obj.greet(); // Lakhveer
πΉ Arrow Function Trap:
const obj = {
name: "JS",
greet: () => {
console.log(this.name);
}
};
obj.greet(); // undefined
π‘ Arrow functions donβt have their own this
π§© 8. Prototypes & Inheritance
JavaScript uses prototype-based inheritance
πΉ Example:
function Person(name) {
this.name = name;
}
Person.prototype.sayHi = function () {
console.log("Hi " + this.name);
};
const p1 = new Person("Rajput");
p1.sayHi();
π‘ Every object has a hidden [[Prototype]]
π§ 9. Deep vs Shallow Copy
πΉ Shallow Copy:
const obj1 = { a: 1 };
const obj2 = obj1;
obj2.a = 2;
console.log(obj1.a); // 2 β
πΉ Deep Copy:
const obj1 = { a: 1 };
const obj2 = JSON.parse(JSON.stringify(obj1));
π‘ Shallow copy shares reference
π― 10. Debouncing & Throttling (Performance Boost π)
πΉ Debouncing:
Executes after delay
function debounce(fn, delay) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), delay);
};
}
πΉ Throttling:
Executes at fixed intervals
π‘ Used in:
- Search inputs
- Scroll events
π€― Surprising JavaScript Hacks & Tricks
π₯ 1. Swap Variables Without Temp
let a = 5, b = 10;
[a, b] = [b, a];
π₯ 2. Remove Duplicates from Array
const arr = [1, 2, 2, 3];
const unique = [...new Set(arr)];
π₯ 3. Convert String to Number Fast
let num = +"123"; // 123
π₯ 4. Short Circuiting Trick
const name = userName || "Guest";
π₯ 5. Optional Chaining (Safe Access)
const user = {};
console.log(user?.profile?.name);
π₯ 6. Nullish Coalescing
let value = null ?? "Default"; // Default
π₯ 7. Flatten Array
const arr = [1, [2, [3]]];
console.log(arr.flat(Infinity));
π₯ 8. Faster Array Creation
Array.from({ length: 5 }, (_, i) => i);
π₯ 9. Object Property Shorthand
const name = "Rajput";
const obj = { name };
π₯ 10. Dynamic Object Keys
const key = "age";
const obj = {
[key]: 25
};
π Pro Developer Tips
β
Always prefer let & const over var
β
Use === instead of ==
β
Avoid global variables
β
Use modular code (ES Modules)
β
Learn debugging tools (Chrome DevTools π)
β
Write clean, readable code β¨
β οΈ Common Mistakes to Avoid
β Misusing this
β Ignoring async behavior
β Not understanding closures
β Overusing global scope
β Forgetting error handling in promises
π― Final Thoughts
JavaScript is full of hidden gems π and powerful concepts. Once you master these:
π Youβll write cleaner, faster, and smarter code π Debugging becomes easier π You stand out as a top-tier developer π
π¬ Pro Tip: βMaster the fundamentals, and frameworks will follow you automatically.β
© Lakhveer Singh Rajput - Blogs. All Rights Reserved.