JavaScript is a high-level, single-threaded, interpreted language with JIT compilation, used to build dynamic and interactive web applications. Hereβs a concise guide for frontend developers to ace interviews.
β‘ Why JavaScript is Fast Today
- JIT Compilation: Converts JS into optimized machine code at runtime.
- Optimized Garbage Collection: Efficient memory management.
πΉ How JavaScript Runs
- Runs inside a JavaScript engine (e.g., V8).
- Code β Parsed β Converted to bytecode β Optimized via JIT.
- Executes via Call Stack and handles async tasks using the Event Loop.
πΉ Core Concepts
Scopes
Function Scope β var
Block Scope β let & const
Hoisting
- Variables & function declarations are moved to the top.
- Functions fully initialized, variables initialized with undefined (var) or TDZ (let/const).
Closures
- Inner functions have access to outer variables even after outer function finishes execution.
Event Loop
- JS is single-threaded. Event Loop allows non-blocking operations (e.g., API calls, timers).
Garbage Collection & Memory Leak
- GC automatically frees memory for unreachable objects.
- Memory leaks occur when objects remain referenced.
this, call, apply, bind
function greet(age) { console.log(this.name + age); }
greet.call(obj, 25);
greet.apply(obj, [25]);
const boundFn = greet.bind(obj);
TDZ (Temporal Dead Zone)
- Accessing let/const before declaration β ReferenceError.
πΉ Tools & Modern JS
Babel β Runs modern JS in older browsers
Webpack β Bundles multiple files into optimized, browser-ready files
Strict Mode β Enforces stricter parsing & error handling
πΉFunctional Programming
Currying
function add(a) {
return function(b) { return a + b; };
}
add(2)(3); // 5
Higher-Order Functions
- Functions that take or return other functions.
Arrow vs Normal Functions
- Arrow: No this, no arguments, cleaner syntax.
Resolve cross browser compatibility issue
- We can use tools like Babel thorough testing, and progressive enhancement to make sure expected behavior across the browser.
πΉ Async JavaScript
Promises
const myPromise = new Promise((resolve, reject) => {
let success = true;
success ? resolve("Success") : reject("Fail");
});
Promise Chaining
fetchUser()
.then(user => fetchOrders(user.id))
.then(orders => fetchPayments(orders))
.then(payments => console.log(payments))
.catch(err => console.error(err));
async/await vs Promise Chaining
- async/await β cleaner syntax, easier error handling (try/catch)
Microtasks vs Callback Queue
- Microtasks β .then, .catch, .finally (higher priority)
- Callbacks β setTimeout, setInterval
Race Condition
- Multiple async operations compete unpredictably.
Debounce vs Throttle
- Debounce β executes after delay
- Throttle β limits execution rate
πΉ Arrays & Iteration
Map vs forEach
- map() β returns a new array, chainable, used in React JSX
- forEach() β returns undefined, for side-effects only
slice vs splice
const arr = [1,2,3,4,5];
arr.slice(1,4); // [2,3,4], original unchanged
arr.splice(1,2); // removes 2 elements, modifies original
πΉ Object Copying
Shallow Copy
const person = { name: "Jenish", address: { city: "Mumbai" } };
const copy = { ...person };
copy.name = "Darshan";
copy.address.city = "Delhi"; // original affected
Deep Copy
const deepCopy = JSON.parse(JSON.stringify(person));
deepCopy.address.city = "Delhi"; // original untouched
πΉ Execution Context
- Creation Phase β variables/functions are hoisted
- Execution Phase β code runs step by step
Top comments (0)