Javascript Interview Question and Answer 1
Question | Answer |
---|---|
What is JavaScript? | - High-level, interpreted scripting language. |
- Makes web pages interactive. | |
- One of the web's three main pillars: HTML, CSS, and JavaScript. | |
Differentiate between Java and JavaScript. | - Java: compiled, object-oriented programming language. |
- JavaScript: interpreted scripting language for web development. | |
- Two distinct languages. | |
What are 'undefined' and 'null' in JavaScript? | - Represent absence of value. |
- 'undefined': variable declared but not yet assigned a value. | |
- 'null': assignment value representing no value or object. | |
What is the use of 'this' keyword in JavaScript? | - Refers to the object it belongs to. |
- Can refer to different objects depending on context. | |
What's a closure in JavaScript? | - Function accessing its own scope, outer function's scope, and global scope. |
What is the difference between '==' and '==='? | - '==': checks value equality with type coercion. |
- '===': checks both value and type equality without type coercion. | |
What are 'truthy' and 'falsy' values? | - 'truthy': evaluates to true in a boolean context. |
- 'falsy': evaluates to false. Includes 0, "", null, undefined, NaN, and false. | |
What's an Immediately Invoked Function Expression (IIFE)? | - Function that runs as soon as it's defined: (function(){ /* code */ })(). |
What are JavaScript data types? | - Undefined, Null, Boolean, String, Symbol, Number, and Object. |
What is a callback function? | - Function passed into another function as an argument. |
- Executed after the outer function completes. | |
What's 'hoisting'? | - JavaScript mechanism moving variables and function declarations to the top of their scope. |
What is the difference between let, const, and var? | - var: function-scoped. |
- let and const: block-scoped. | |
- const: cannot be reassigned. | |
Explain event delegation. | - Single event listener on a common ancestor of elements to catch events as they bubble up. |
What is the Document Object Model (DOM)? | - Tree-like structure representing content of a webpage. |
- Can be manipulated by JavaScript. | |
What's the difference between null and undefined? | - null: intentional absence of value. |
- undefined: variable declared but not yet assigned a value. | |
What's the difference between a method and a function? | - Function: block of reusable code. |
- Method: function associated with an object. | |
What is a Promise? | - Represents a value: available now, in the future, or never. |
- Has states: pending, fulfilled, rejected. | |
What's the difference between window, screen, and document in JavaScript? | - window: global object for browser's window. |
- screen: information about user's screen. | |
- document: represents the DOM. | |
What is AJAX? | - Asynchronous JavaScript And XML. |
- Technique for dynamic web pages; makes server requests without reloading the page. | |
What's a cross-origin request? | - Request for a resource from a different domain, protocol, or port due to same-origin policy. |
How does bind() work? | - Creates a new function with a specific context for the this value. |
What's the difference between apply(), call(), and bind()? | - All set the this value. |
- call() and apply(): invoke function immediately. | |
- apply(): accepts array of arguments. | |
- bind(): returns new function with specified this value. | |
What are arrow functions? | - Introduced in ES6: concise way to write functions. |
- Don't bind their own this value. | |
What's a prototype in JavaScript? | - Every object has a prototype for inheritance. |
What's the difference between a static method and an instance method? | - Static: belongs to the class. |
- Instance: belongs to the object instance, accesses instance data. | |
What's the event loop? | - Handles asynchronous operations. |
- Checks if call stack is empty; if so, adds functions from the event queue. | |
What are JavaScript timers? | - setTimeout() and setInterval(): execute a function after a set period. |
How can you convert a string to a number? | - parseInt(), parseFloat(), unary + operator, or Number() function. |
How do you clone an object in JavaScript? | - Object.assign(), spread operator (...). |
- Deep clone: libraries like lodash or JSON.parse(JSON.stringify(object)). | |
What's the difference between a shallow copy and a deep copy? | - Shallow: clones outermost object. |
- Deep: clones entire hierarchy of objects. | |
What is a JavaScript class? | - Blueprint for creating objects. |
- Introduced in ES6; supports inheritance, constructors, methods. | |
What's the difference between forEach and map? |
Question | Answer |
---|---|
What's a prototype in JavaScript? | - Every JavaScript object has a prototype. <br> - It inherits properties from this prototype. <br> - Mechanism for achieving inheritance. |
What is AJAX? | - Stands for Asynchronous JavaScript And XML. <br> - Technique for creating fast, dynamic web pages. <br> - Allows requests to server without reloading entire page. |
What is the Document Object Model (DOM)? | - Tree-like structure representing content of a webpage. <br> - Can be manipulated using JavaScript. |
How does bind() work? | - bind() creates a new function. <br> - Sets a certain context for the this value. |
What are arrow functions? | - Introduced in ES6. <br> - Concise way to write functions. <br> - Doesn't bind its own this value. |
What's the difference between splice and slice? | - splice: modifies original array (add/remove elements). <br> - slice: returns portion of array without modifying original. |
What is a JavaScript class? | - Introduced in ES6. <br> - Blueprint for creating objects. <br> - Supports inheritance, constructors, and methods. |
What are template literals in JavaScript? | - Introduced in ES6. <br> - Allows for embedded expressions and string interpolation. <br> - Uses backticks ( ). |
What is destructuring in JavaScript? | - Allows binding of parts of data structures (arrays or objects) to variables. |
What is the difference between target and currentTarget in event context? | - target: element where event occurred. <br> - currentTarget: element with the event listener attached. |
How do you add an event listener to a DOM element? | - Using the addEventListener method. |
What is a JavaScript generator? | - Function that can pause/resume its execution. <br> - Introduced in ES6. <br> - Uses function* syntax and yield keyword. |
Explain the concept of a "single page application" (SPA). | - Web application that rewrites current page for interaction. <br> - Doesn't load new pages from the server. |
How can you share code between files in JavaScript? | - Using modules with import and export statements. |
What is the async/await syntax useful for? | - Cleaner way to handle asynchronous operations. <br> - Makes them appear synchronous. |
What's the difference between a "function declaration" and a "function expression"? | - Function declaration: defines named function, no variable assignment. <br> - Function expression: assigns unnamed function to a variable. |
What is Babel? | - JavaScript compiler/transpiler. <br> - Converts next-gen JavaScript to older versions for browser compatibility. |
How do you handle errors in Promises? | - Use .catch() method. <br> - Or use try/catch with async/await . |
How do you prevent a function from being called multiple times in a row? | - Use debouncing or throttling techniques. |
What is transpiling? | - Converting source code from one language/version to another. <br> - E.g., from ES6 to ES5 for browser compatibility. |
What are the benefits of using TypeScript? | - Typed superset of JavaScript. <br> - Provides type safety, better refactoring, early bug detection. |
How do closures work in JavaScript? | - Function with access to the parent scope even after parent function has closed. <br> - Allows data privacy and emulating private methods. |
How can you make a JavaScript function sleep or wait? | - Use setTimeout function. <br> - Or use async/await with a promise-based delay. |
What is a Set in JavaScript? | - ES6 collection of values. <br> - Each value must be unique. |
What is the purpose of the data- attribute in HTML? | - Stores custom data private to the page or application. <br> - In JavaScript, can access using the dataset property. |
What is CORS? | - Stands for "Cross-Origin Resource Sharing". <br> - Browser security feature controlling requests for resources from different origins. |
What is JSON? | - Stands for JavaScript Object Notation. <br> - Lightweight data-interchange format. <br> - Easy for humans to read/write. |
What are JavaScript cookies? | - Small pieces of data stored on user's browser. <br> - Used to track actions or save state. |
What is local storage? | - Web storage for storing key-value pairs in a web browser. <br> - Data persists with no expiration time. |
What's the difference between local storage and session storage? | - Local storage: persists data after browser closes. <br> - Session storage: retains data for one session, deleted after browser closes. |
Question | Answer |
---|---|
What's the difference between forEach and map ? | - forEach : iterates over elements; doesn't return new array. <br> - map : iterates and transforms each element; returns new array. |
How does the filter method work in JavaScript? | - Iterates over array. <br> - Returns new array with elements passing a test (predicate function). |
Describe the reduce method in JavaScript. | - Iterates over array. <br> - Accumulates array values into a single value. <br> - Takes a reducer and initial value as arguments. |
What is a Promise in JavaScript? | - Object representing eventual completion or failure of async operation. <br> - Has three states: pending, resolved, and rejected. |
How does the finally method work with Promises? | - Executes after promise is either fulfilled or rejected. <br> - Doesn't receive promise results. |
What are callback functions? | - Functions passed as arguments to another function. <br> - Executed after the outer function completes. |
What is event delegation? | - Technique of adding event listener to common parent. <br> - Relies on event propagation to detect events on child elements. |
Describe the this keyword in JavaScript. | - Refers to object executing current function. <br> - Context depends on invocation (e.g., global, object, event). |
How do you clone an object in JavaScript? | - Using Object.assign() . <br> - Or using spread operator (... ). |
What's the difference between == and === ? | - == : loose equality, type coercion can occur. <br> - === : strict equality, no type coercion. |
What are the different types of JavaScript loops? | - for , while , do...while , for...in , for...of . |
Describe variable hoisting. | - Variables declared with var are "moved" to top of their scope. <br> - Initialization remains where it is. |
Question | Answer |
---|---|
What's the difference between forEach and map ? | - forEach : Iterates over array elements, doesn't return a new array. <br> - map : Iterates and creates a new array based on the function provided.
|
Question | Answer |
---|---|
What's the difference between forEach and map ? | - forEach : iterates over elements; doesn't return new array. <br> - map : iterates and transforms each element; returns new array. |
How does the filter method work in JavaScript? | - Iterates over array. <br> - Returns new array with elements passing a test (predicate function). |
Describe the reduce method in JavaScript. | - Iterates over array. <br> - Accumulates array values into a single value. <br> - Takes a reducer and initial value as arguments. |
What is a Promise in JavaScript? | - Object representing eventual completion or failure of async operation. <br> - Has three states: pending, resolved, and rejected. |
How does the finally method work with Promises? | - Executes after promise is either fulfilled or rejected. <br> - Doesn't receive promise results. |
What are callback functions? | - Functions passed as arguments to another function. <br> - Executed after the outer function completes. |
What is event delegation? | - Technique of adding event listener to common parent. <br> - Relies on event propagation to detect events on child elements. |
Describe the this keyword in JavaScript. | - Refers to object executing current function. <br> - Context depends on invocation (e.g., global, object, event). |
How do you clone an object in JavaScript? | - Using Object.assign() . <br> - Or using spread operator (... ). |
What's the difference between == and === ? | - == : loose equality, type coercion can occur. <br> - === : strict equality, no type coercion. |
What are the different types of JavaScript loops? | - for , while , do...while , for...in , for...of . |
Describe variable hoisting. | - Variables declared with var are "moved" to top of their scope. <br> - Initialization remains where it is. |
Comments
Post a Comment