JavaScript Explained 10 questions

  1. What is JavaScript hoisting?
    • Answer: JavaScript hoisting is the default behavior of moving all the declarations to the top of the scope (the top of the script or the current function) before the code execution.
    • Explanation: In JavaScript, variables and function declarations are moved to the top of their containing scope, before the code execution begins. However, only declarations are hoisted, not initializations. If a variable is declared and initialized after using it, the value will be undefined.
  2. Explain the difference between == and === in JavaScript.
    • Answer: == is the loose equality operator, which performs type coercion if the variables being compared are of different types. === is the strict equality operator, which does not perform type coercion.
    • Explanation: When using ==, JavaScript converts the types of the variables if needed. For example, "5" == 5 will return true. When using ===, both the value and the type must match for the comparison to return true. Thus, "5" === 5 will return false.
  3. What are JavaScript closures and how do they work?
    • Answer: A closure is a function that has access to its outer function scope even after the outer function has returned. This means a closure can remember and access variables and arguments of its outer function even after the function has finished.
    • Explanation: Closures are created every time a function is created, at function creation time. The inner function will retain the scope chain of the enclosing function at the time the enclosing function was executed, hence it can access the outer function’s variables.
  4. What is the ‘this’ keyword in JavaScript?
    • Answer: In JavaScript, this is a keyword that refers to the object it belongs to.
    • Explanation: The value of this is determined by how a function is called. It has different values depending on where it is used: In a method, this refers to the owner object. Alone, this refers to the global object. In a function, in strict mode, this is undefined, whereas in non-strict mode, it refers to the global object. In an event, this refers to the element that received the event.
  5. What is event bubbling and event capturing in JavaScript?
    • Answer: Event bubbling and event capturing are two ways of event propagation in the HTML DOM API when an event occurs in an element inside another element.
    • Explanation: In event bubbling, the event is first captured and handled by the innermost element and then propagated to outer elements. In event capturing, the event is first captured by the outermost element and propagated to the inner elements. JavaScript allows you to add event listeners to capture or bubble phase.
  6. What is the purpose of the Array map() function?
    • Answer: The map() function in JavaScript is used to iterate over an array and apply a function on every element of the array, returning a new array without altering the original array.
    • Explanation: map() takes a callback function as an argument and applies it to every element in the array, then returns a new array with the results. This is useful for transforming data in arrays without modifying the original data.
  7. Explain the difference between var, let, and const in JavaScript.
    • Answer: var is function-scoped, let and const are block-scoped. var variables can be re-declared and updated, let variables can be updated but not re-declared, const variables can neither be updated nor re-declared.
    • Explanation: var declarations are globally scoped or function scoped while let and const are block scoped. var variables can be updated and re-declared within its scope; let variables can be updated but not re-declared; const variables can neither be updated nor re-declared.
  8. What is a Promise in JavaScript?
    • Answer: A Promise in JavaScript is an object representing the eventual completion or failure of an asynchronous operation.
    • Explanation: A promise is used to handle the asynchronous result of an operation. JavaScript is single-threaded, meaning that it can only execute one code block at a time. A promise is an object that gives us the result of an asynchronous operation. It can be in one of three states: Pending, Fulfilled, or Rejected.
  9. Explain the concept of prototypal inheritance in JavaScript.
    • Answer: Prototypal inheritance is a feature in JavaScript used to add methods and properties in objects. It is a method by which an object can inherit the properties and methods of another object.
    • Explanation: Traditionally, in order to get and set the [[Prototype]] of an object, we use Object.getPrototypeOf and Object.setPrototypeOf. JavaScript objects have a link to a prototype object. When trying to access a property of an object, the search starts with the object itself and if the property is not found, it continues with the prototype of the object, and so on, until the property is found or the prototype chain is exhausted.
  10. What is the event loop in JavaScript?
    • Answer: The event loop is a mechanism that allows JavaScript to perform non-blocking asynchronous operations, despite being single-threaded, by offloading operations to the system kernel whenever possible.
    • Explanation: JavaScript has a concurrency model based on an event loop, which is responsible for executing the code, collecting and processing events, and executing queued sub-tasks. This model allows JavaScript to perform non-blocking I/O operations, despite JavaScript being single-threaded, by using callback functions that are executed at a later time once the response (from DB, API, file system, etc.) has been received.