How can you handle potential undefined values when destructuring an object?
- Ignore the value
- Provide default values
- Throw an error
- Use try-catch block
When destructuring an object, you can provide default values to handle potential undefined values. If the property is not present in the object, the default value will be used. This helps prevent errors due to missing properties.
Can a for...of loop be used to iterate over a generator function's yielded values?
- Yes, a for...of loop can iterate over the yielded values of a generator function.
- No, for...of loops are not compatible with generator functions.
- Yes, but it requires additional syntax.
- No, generator functions can only be iterated using for...in loops.
Yes, a for...of loop can be used to iterate over a generator function's yielded values. The for...of loop automatically iterates over the values yielded by the generator, making it a convenient and readable way to consume generator values.
In ES6, class properties are often initialized inside the ________ method.
- initialize
- constructor
- create
- new
Class properties in ES6 are commonly initialized inside the constructor method. This allows you to set initial values for properties when an object is created.
In which scenarios are dynamic imports particularly useful?
- Small Applications
- Large Applications with Code Splitting Needs
- Static Applications
- Mobile Applications Only
Dynamic imports are particularly useful in large applications where code splitting is necessary. This allows loading only the required modules on-demand, reducing the initial load time and improving overall application performance. Small and static applications may not benefit as much from dynamic imports.
When a function returns a new object each time it is called, even with the same inputs, is it considered a pure function?
- Yes
- No
- Depends on the type of object
- Only if the object is immutable
A pure function should consistently return the same result for the same inputs, and creating new objects introduces unpredictability, making it impure.
In ES6, __________ methods allow for concise method definitions without the function keyword.
- Arrow
- Define
- Class
- Prototype
In ES6, arrow functions provide a concise syntax for defining functions. The arrow (=>) is used to declare arrow functions, and they are particularly useful for defining methods on objects without the need for the function keyword. Arrow functions inherit the this value from the enclosing scope, making them convenient for defining methods within ES6 classes.
In what scenarios is it not recommended to use arrow functions in JavaScript?
- In event handlers
- In methods of classes
- In functions with dynamic this
- In callback functions within a setTimeout
Arrow functions do not have their own 'this' context and inherit it from the enclosing scope. This can lead to unexpected behavior in functions that rely on the dynamic binding of 'this'. It is not recommended to use arrow functions in functions with dynamic 'this' requirements.
Is it possible to call a static method from another static method within the same class?
- No, it leads to a syntax error.
- Yes, using the class name.
- No, static methods cannot call each other.
- Yes, using the this keyword.
Yes, it is possible to call a static method from another static method within the same class using the class name. Static methods are associated with the class and can be called using the class name to avoid confusion. Option B is correct.
Which statement correctly initiates a dynamic import in ES6?
- import module from 'module'
- require('module')
- import('module')
- require.ensure(['module'], callback)
The correct syntax for initiating a dynamic import in ES6 is import('module'). This allows you to asynchronously load and work with modules at runtime, enhancing flexibility in the application structure.
Tree shaking can fail to eliminate unused code if modules are dynamically _______ at runtime.
- Imported
- Exported
- Loaded
- Unloaded
The correct option is (c) Loaded. Tree shaking may fail if modules are dynamically loaded at runtime. Dynamically loading modules introduces uncertainty about which modules will be used, making it challenging for the module bundler to perform effective tree shaking.