What is the key feature of enhanced object literals in ES6 that allows properties to be set more concisely?

  • Shorthand property notation
  • Computed property names
  • Method shorthand notation
  • Default parameter values
In enhanced object literals, shorthand property notation allows concise property assignment. This means you can omit the repetition of the property name if it matches the variable name. This enhances code readability.

In an application dealing with an array of user objects, which higher-order function would be best for filtering users based on specific criteria?

  • map
  • filter
  • reduce
  • forEach
The correct option is filter. filter is specifically designed for filtering elements in an array based on a given criteria. It creates a new array with only the elements that satisfy the provided function.

How does tree shaking contribute to the final bundle size in a JavaScript project?

  • Increases the bundle size
  • Has no impact on the bundle size
  • Decreases the bundle size by removing unused code
  • Only affects the bundle size for server-side code
Tree shaking significantly reduces the final bundle size by identifying and excluding unused or dead code. It helps in delivering a more optimized and lightweight application to end-users. Developers can achieve a smaller bundle size, leading to faster load times and improved overall performance, especially in resource-intensive web applications.

What is the purpose of the map method in JavaScript arrays?

  • Iterates over each element, allowing you to modify them and returns a new array with the results.
  • Removes elements that do not satisfy a certain condition and returns a new array.
  • Reduces the array to a single value based on a provided function.
  • Returns the first element that satisfies a given condition.
The map method is used to iterate over each element in the array, apply a function to each element, and return a new array with the results. It doesn't modify the original array. This is commonly used for transforming data in arrays.

The spread operator is used to ________ elements of an iterable into individual elements.

  • Combine
  • Separate
  • Flatten
  • Merge
The spread operator (...) is used to separate the elements of an iterable, like an array, into individual elements. This is often used to spread elements into a new array or function arguments.

How do getter and setter methods in ES6 classes enhance object property access?

  • They allow direct modification of private properties
  • They enable the use of arrow functions
  • They provide a way to control access to object properties
  • They are only used for static properties
In ES6 classes, getter and setter methods allow controlled access to object properties. Getters retrieve the value, and setters modify the value, providing a way to implement data validation or custom behavior during property access or modification.

How do arrow functions behave differently from traditional functions in terms of hoisting?

  • Arrow functions are not hoisted
  • Arrow functions are hoisted with their entire body
  • Arrow functions are hoisted but only the function declaration
  • Arrow functions are hoisted with their parameters
Arrow functions are not hoisted, unlike traditional functions. They must be defined before they are used. Option A is correct.

In ES6, what happens when the spread operator is used with an iterable like a string?

  • Spreads each character into an array
  • Concatenates the string
  • Reverses the string
  • Throws an error
When the spread operator (...) is used with an iterable like a string, it spreads each character of the string into an array. This allows you to easily convert a string into an array of characters in ES6.

How can the spread operator facilitate the process of adding elements to state arrays in a React component without mutating the original state?

  • Create a new array using the spread operator, add elements, and set the state
  • Use the Array.push() method to add elements to the state array
  • Directly modify the existing state array
  • Utilize the Array.concat() method to add elements to the state array
The spread operator is used in React to avoid mutating the state directly. Creating a new array with the spread operator, adding elements, and then setting the state ensures immutability and proper state management in React components.

Can you define currying in JavaScript?

  • A technique of transforming a function with multiple arguments into a sequence of functions
  • A method to manipulate arrays in JavaScript
  • A way to create objects using constructors
  • An approach to handling asynchronous code
Currying is a technique in JavaScript that involves transforming a function with multiple arguments into a sequence of functions, each taking a single argument. This allows for partial application of the function, making it more flexible and reusable. Currying is particularly useful in functional programming and can lead to more concise and modular code. A solid understanding of currying is beneficial for developers working with JavaScript.