2. Explain the difference between
null is a deliberate assignment of a non-value, while
undefined means a variable has been declared but has not been assigned any value.
3. What is the DOM?
Answer: The DOM (Document Object Model) is a programming interface for web documents. It represents the structure of a document as a tree of objects, allowing scripts to dynamically update the content, structure, and style of a document.
Answer: You can use the
typeof operator. Example:
Answer: Closures are functions that have access to variables from their outer (enclosing) scope, even after the outer function has finished executing.
7. What is the difference between
const are block-scoped, while
var is function-scoped.
const is used for constants, and
let allows variable reassignment.
8. Explain the concept of prototypal inheritance.
null prototype is reached.
9. What is the purpose of the
this refers to the object to which a function or method belongs. Its value depends on how a function is called.
10. What is the difference between
== checks for equality after type coercion, while
=== checks for equality without type coercion (strict equality).
Answer: Hoisting is the behavior where variable and function declarations are moved to the top of their containing scope during compilation.
12. What is a callback function?
Answer: A callback function is a function passed as an argument to another function, which is then invoked inside the outer function.
13. What is the purpose of the
bind method creates a new function that, when called, has its
this keyword set to a specific value, and prepends any provided parameters to the original function.
14. What is the purpose of the
map function is used to create a new array by applying a provided function to each element of an existing array.
Answer: A promise is an object representing the eventual completion or failure of an asynchronous operation. It has methods like
catch to handle the result or error.
16. Explain the concept of the same-origin policy.
Answer: The same-origin policy is a security measure that restricts web pages from making requests to a different domain than the one that served the web page.
17. What is the purpose of the
async keyword is used to declare an asynchronous function, and
await is used to pause the execution of an asynchronous function until the promise is settled.
18. What is the purpose of the
sessionStorage provide a way to store key/value pairs in a web browser.
localStorage persists even when the browser is closed, while
sessionStorage is session-specific.
Answer: Errors can be handled using try…catch blocks, where potentially error-prone code is placed in the
try block, and error handling is done in the
20. Explain the concept of event delegation.
Answer: Event delegation involves using a single event listener to manage all the events for a particular type, instead of attaching listeners to individual elements.
21. What is the difference between the
apply are methods used to invoke a function with a specified
this value, but
call takes individual arguments, while
apply takes an array of arguments.
22. What is the purpose of the
Array.isArray() is used to check if a value is an array.
24. What is the purpose of the
slice method is used to extract a portion of an array and returns a new array without modifying the original.
25. How does prototypal inheritance differ from classical inheritance?
Answer: In prototypal inheritance, objects can inherit directly from other objects, while classical inheritance involves classes and a hierarchy of parent and child classes.
26. What is a RESTful API?
Answer: A RESTful API (Representational State Transfer) is an architectural style for designing networked applications, using standard HTTP methods (GET, POST, PUT, DELETE) to perform CRUD operations.
27. Explain the concept of the “callback hell” and how to avoid it.
Answer: Callback hell refers to the situation where multiple nested callbacks make the code difficult to read. To avoid it, use named functions, modularize code, or use Promises/async-await.
28. What is the purpose of the
event.preventDefault() is used to prevent the default behavior of an event, such as submitting a form or following a link.
29. What is the purpose of the
reduce() method is used to reduce an array to a single value by applying a function to each element, accumulating the result.
Answer: A closure is a function that has access to its own scope, the outer function’s scope, and the global scope, even after the outer function has finished execution.
You May Also Read:
31. What is the purpose of the
Array.filter() is used to create a new array with elements that pass a provided test.
Answer: Event delegation involves attaching a single event listener to a common ancestor of multiple elements. This listener checks the target of the event and performs an action based on that target.
33. What is the purpose of the
setTimeout() is used to schedule the execution of a function or code snippet after a specified delay in milliseconds.
34. Explain the purpose of the
fetch API is used to make asynchronous HTTP requests. It returns a Promise that resolves to the
Response to that request.
35. What is the difference between
let allows reassignment of the variable, while
const does not. Once a value is assigned to a
const variable, it cannot be changed.
36. What is the purpose of the
Array.forEach() is used to iterate over elements of an array and execute a provided function once for each array element.
37. How does arrow function syntax differ from regular function syntax?
Answer: Arrow functions have a shorter syntax and do not bind their own
this. They inherit
this from the enclosing scope.
38. What is the purpose of the
localStorage is a web storage object that allows you to store key/value pairs in a web browser with no expiration time.
39. Explain the concept of the event bubbling and event capturing phases.
40. What is the purpose of the
Array.unshift() is used to add one or more elements to the beginning of an array, and it returns the new length of the array.
42. What is the difference between
localStorage persists even when the browser is closed, while
sessionStorage is specific to a session and is cleared when the browser is closed.
43. What is the purpose of the
Array.reverse() is used to reverse the order of elements in an array.
45. What is the purpose of the
bind method is used to create a new function with a specified
this value and initial parameters, without invoking the function immediately.
Answer: A callback function is a function passed as an argument to another function, which is then executed at a later time, often after an asynchronous operation is complete.
47. What is the purpose of the
Array.every() is used to check if all elements in an array pass a provided test.
"use strict"; at the beginning of a script or a function.
49. What is the purpose of the
Array.concat() is used to merge two or more arrays, creating a new array without modifying the existing arrays.
Answer: There are various methods to deep clone an object, including using
JSON.parse(JSON.stringify(obj)), a custom recursive function, or using libraries like Lodash. Each method has its pros and cons, and the choice depends on the specific use case.