Back to IQs

Javascript Interview Questions

What is JavaScript?
JavaScript is a dynamic, high-level programming language primarily used for creating interactive effects and dynamic content on web pages.
What is the difference between JavaScript and Java?
JavaScript is a scripting language for web development, while Java is a general-purpose, object-oriented programming language used for a wide range of applications, including web servers, mobile apps, and enterprise solutions.
What are closures in JavaScript?
A closure is a function that retains access to its lexical scope, even when executed outside of that scope, allowing for private variables and function factories.
What is event delegation in JavaScript?
Event delegation is a technique in JavaScript where an event listener is attached to a parent element rather than individual child elements, improving performance and managing dynamically added elements.
What is the difference between '==' and '===' in JavaScript?
'==' compares values after converting them to a common type, while '===' compares both values and types without type coercion.
What is the purpose of the 'this' keyword in JavaScript?
The value of 'this' is determined by the call site for ordinary functions: obj.method() supplies obj, while call(), apply(), and bind() can supply an explicit value. Arrow functions do not bind their own 'this'; they capture it lexically from the surrounding scope.
What are promises in JavaScript?
Promises are objects that represent the eventual completion (or failure) of an asynchronous operation, allowing for cleaner handling of asynchronous code.
What is the difference between var, let, and const in JavaScript?
var declares variables with function scope, let declares block-scoped variables, and const declares block-scoped variables with immutable values.
What is the purpose of JavaScript's 'async' and 'await'?
'async' is used to define a function that returns a promise, and 'await' is used to wait for a promise to resolve before continuing execution within an 'async' function.
What is an IIFE in JavaScript?
An IIFE (Immediately Invoked Function Expression) is a function that runs as soon as it is defined, often used to create private scope for variables.
What are JavaScript callbacks?
Callbacks are functions passed as arguments to other functions, which are executed after the completion of an asynchronous operation.
What is the event loop in JavaScript?
The event loop is a mechanism that handles asynchronous operations in JavaScript by continuously checking the message queue and executing pending callbacks.
What is hoisting in JavaScript?
Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of their containing scope during the compile phase.
What are JavaScript modules?
JavaScript modules are reusable blocks of code that can be imported and exported between files, helping to organize and maintain larger codebases.
What is the difference between 'null' and 'undefined' in JavaScript?
'null' is an assignment value representing no value, while 'undefined' means a variable has been declared but has not been assigned a value.
What is a JavaScript generator?
A generator is a function that can yield multiple values over time, pausing and resuming execution using the 'yield' keyword.
What is a 'forEach' loop in JavaScript?
The 'forEach' loop iterates over an array, calling a provided function once for each element in the array, but it doesn't return a value.
What is a promise chain in JavaScript?
A promise chain allows multiple asynchronous operations to be executed in sequence by chaining '.then()' calls, with each step waiting for the previous one to complete.
What is the 'bind' method in JavaScript?
The 'bind' method creates a new function that, when called, has its 'this' value set to a specified object, allowing for better control over context in callback functions.
What is destructuring in JavaScript?
Destructuring allows you to unpack values from arrays or properties from objects into distinct variables, making code more concise and readable.
How does the JavaScript event loop order asynchronous work?
Synchronous code runs first. After the current stack completes, the runtime drains microtasks such as Promise callbacks before taking the next task, such as a timer or UI event. Long synchronous work blocks both rendering and asynchronous callbacks because JavaScript execution is single-threaded per event loop.
What is the difference between debounce and throttle?
Debounce waits until calls stop for a chosen interval, which suits search input. Throttle limits execution to at most once per interval, which suits scroll or resize handling. Both reduce work but do not replace cancellation, pagination, or server-side rate limiting.
How do you prevent race conditions in browser data fetching?
Cancel obsolete requests with AbortController or associate each response with the latest request before updating state. Also handle loading, error, empty, and cancellation states explicitly; otherwise a slower earlier response can overwrite newer data.
What are common web security risks in JavaScript applications?
Treat all external input as untrusted, prevent XSS through contextual escaping and safe DOM APIs, protect state-changing requests against CSRF when using cookies, and avoid exposing secrets in frontend bundles. Use secure headers, dependency auditing, and server-side authorization as complementary controls.
When should you use Web Workers?
Use a Web Worker for CPU-heavy work such as parsing, image processing, or large calculations that would block the main thread. Workers communicate by messages and cannot directly access the DOM, so the transfer or cloning cost should be justified by the work saved.
How do you improve Core Web Vitals?
Measure real-user data first, then reduce render-blocking work and optimize the critical resource for LCP, reserve space and dimensions to protect CLS, and reduce long main-thread tasks and input latency for INP. Validate changes in field data as well as local lab tools because device and network conditions differ.
How does AbortController improve frontend request handling?
AbortController lets code cancel fetch requests and other abort-aware operations when a component unmounts, a newer search replaces an older one, or a deadline expires. Cancellation should be treated separately from a server failure so the UI does not show a misleading error for an intentional abort.
What is Content Security Policy used for?
Content Security Policy limits where scripts, styles, images, and other resources may load from, reducing the impact of injection vulnerabilities. Roll it out with report-only monitoring, remove unsafe allowances where possible, and treat CSP as a defense-in-depth control rather than a substitute for output encoding.