Java-Script Reference

Links : Learn     Interview Questions     IDE
            

JavaScript Interview Questions - Page 2

< Previous Page              Next Page >
Question: How does event delegation work in JavaScript?
Answer: The event delegation is a technique in JavaScript where instead of attaching event handlers to individual elements, a single event handler is attached to a parent element. This allows you to manage events for multiple child elements using a single event listener.

When an event occurs, it bubbles up through the DOM hierarchy, and the parent element's event handler can check the target of the event to determine which child element triggered the event.

Question: What are the different ways to create objects in JavaScript?
Answer: Objects in JavaScript can be created using object literal syntax, constructor functions, the Object.create() method, or ES6 classes:
• Object literal syntax involves defining an object using curly braces '{}'
• Constructor functions use the 'new' keyword to create instances
• The 'Object.create()' creates a new object with a specified prototype
• And ES6 classes provide syntactic sugar over constructor functions for creating objects.

Question: How does the event loop work in JavaScript?
Answer: The event loop is a central component of JavaScript's runtime environment responsible for handling asynchronous operations. It continuously checks the call stack for tasks to execute. If the call stack is empty, it checks the task queue for any pending tasks. If there are tasks in the queue, the event loop moves them to the call stack for execution.

This process continues as long as the application is running, allowing JavaScript to handle asynchronous tasks in a non-blocking manner.

Question: What are arrow functions in JavaScript?
Answer: The arrow functions are a shorter syntax for writing function expressions in JavaScript, introduced in ES6. They have a more concise syntax compared to traditional function expressions and lexically bind the this value, meaning they inherit the this value from the surrounding code.

Question: Can you explain event bubbling and event capturing in JavaScript?
Answer: The event bubbling is a phase in the event propagation model where an event triggered on a specific element will propagate up the DOM hierarchy, triggering handlers on ancestor elements.

Event capturing is the opposite, where the event is captured on the root element and then propagates down the DOM hierarchy until it reaches the target element.

Question: What is a callback function?
Answer: A callback function is a function passed as an argument to another function to be executed later when a specific event occurs or when a particular task is completed.

Callbacks are commonly used in asynchronous programming to handle tasks such as handling responses from APIs or performing actions after asynchronous operations complete.

Question: Explain the difference between 'document.ready' and 'window.onload'?
Answer: The 'document.ready' is an event in jQuery that fires when the DOM is fully loaded and can be manipulated, whereas 'window.onload' is a native JavaScript event that fires when all resources (including images and scripts) have been loaded.

< Previous Page Next Page >