DEV Community

Jenish Dabhi
Jenish Dabhi

Posted on

🟑 JavaScript Interview Preparation Notes – Key Concepts & Q/A

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);
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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");
});

Enter fullscreen mode Exit fullscreen mode

Promise Chaining

fetchUser()
  .then(user => fetchOrders(user.id))
  .then(orders => fetchPayments(orders))
  .then(payments => console.log(payments))
  .catch(err => console.error(err));

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Object Copying

Shallow Copy

const person = { name: "Jenish", address: { city: "Mumbai" } };
const copy = { ...person };

copy.name = "Darshan";
copy.address.city = "Delhi"; // original affected

Enter fullscreen mode Exit fullscreen mode

Deep Copy

const deepCopy = JSON.parse(JSON.stringify(person));
deepCopy.address.city = "Delhi"; // original untouched

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Execution Context

  • Creation Phase β†’ variables/functions are hoisted
  • Execution Phase β†’ code runs step by step

Top comments (0)