The mechanism of closing over variables is a core concept in ________ programming in JavaScript.

  • Functional
  • Object-Oriented
  • Asynchronous
  • Procedural
The mechanism of closing over variables is a core concept in "Functional" programming in JavaScript. Functional programming promotes the use of pure functions and emphasizes the importance of avoiding mutable state. Closures play a vital role in functional programming by allowing functions to capture and remember the surrounding state.

To iterate over the entries of an object (key-value pairs), the object should be converted to an array of arrays using Object._______.

  • entries
  • toArray
  • keys
  • values
To iterate over the entries (key-value pairs) of an object, the object should be converted to an array of arrays using Object.entries. This method returns an array containing arrays, each with two elements: the key and its corresponding value from the object. You can then iterate over these pairs.

You've encountered a "Callback Hell" in a project you've inherited. What could be a strategic approach to refactor and manage the nested callbacks for better readability and maintainability?

  • Refactor using named functions
  • Continue using nested callbacks
  • Use anonymous arrow functions
  • Convert callbacks to Promises
When dealing with "Callback Hell," the strategic approach is to refactor the code using named functions. This technique makes the code more readable and maintainable by breaking down nested callbacks into separate named functions. It enhances code structure and comprehensibility, making it easier to manage complex asynchronous logic.

Can arrow functions be used as constructors?

  • No
  • Yes
  • Only in ES6
  • Only in ES5
No, arrow functions cannot be used as constructors in JavaScript. Unlike regular functions, arrow functions do not have their own 'this' binding, and they cannot be used with the 'new' keyword to create instances of objects. Arrow functions are primarily used for creating concise function expressions.

Which property is used to change the text content of a selected element?

  • innerText
  • textContent
  • innerHTML
  • text
The textContent property is used to change the text content of a selected element. It sets or returns the text content of an element and ensures that any HTML tags are treated as plain text. The other options do not handle text content in the same way.

What is a property in JavaScript objects?

  • A value associated with an element in an array
  • A key-value pair associated with an object
  • A function that performs an action
  • A variable declaration
A property in JavaScript objects is a key-value pair associated with an object. The key is a string (or Symbol), and the value can be of any data type. Properties allow you to store data and functions within an object, making it a fundamental concept in JavaScript's object-oriented programming.

To prevent variables from being added to the global object, it is common to use a(n) _______ function expression.

  • IIFE (Immediately Invoked Function Expression)
  • Arrow
  • Anonymous
  • Prototype
To prevent variables from polluting the global object, an IIFE (Immediately Invoked Function Expression) is commonly used. It encapsulates code within a function scope and immediately executes it, keeping variables private and not accessible from the global scope.

The method _______ can be used to add new elements to the end of an array.

  • push()
  • unshift()
  • add()
  • append()
The push() method in JavaScript can be used to add new elements to the end of an array. This method modifies the original array and is commonly used when you want to add elements to the end of an array, expanding its length. For example, myArray.push('newElement') would add 'newElement' to the end of myArray.

What value types can be used for case comparisons in a switch statement?

  • Strings, numbers, and symbols
  • Arrays, objects, and booleans
  • Only numbers and booleans
  • Functions and undefined values
In a switch statement, you can use strings, numbers, and symbols as values for case comparisons. These values are compared strictly, meaning both value and type must match for the associated code block to execute.

In what scenario might you prefer to use a function expression over an arrow function?

  • When needing a concise syntax.
  • When you want to bind this explicitly.
  • When working with callbacks in event handling.
  • When using async/await for asynchronous code.
You might prefer to use a function expression (a regular function) over an arrow function when you need to explicitly bind the this context, especially in cases where you want to define methods inside objects or use constructors. Function expressions allow you to use the this keyword as expected, while arrow functions inherit this from their lexical enclosing context.