Which of the following scenarios is NOT recommended for using arrow functions?
- As event handlers in the DOM.
- As methods within objects.
- In callbacks where "this" context matters.
- In short, simple functions.
Arrow functions are not recommended for use as event handlers in the DOM. This is because the value of "this" in an arrow function is not determined by the event but instead retains the value from its enclosing lexical context, which may not be what you expect in an event handler. Traditional functions are usually preferred for event handlers.
How does JavaScript support lexical scoping?
- By creating global variables
- Through the use of the 'var' keyword
- By nesting functions
- By using block-level scoping with 'let'
JavaScript supports lexical scoping by allowing functions to access variables defined in their containing (enclosing) functions. This means that inner functions can "see" and use variables from outer functions, creating a chain of scope. This is achieved by nesting functions. Modern JavaScript also introduced 'let' for block-level scoping.
Which keyword is used to check a condition in JavaScript?
- for
- if
- while
- switch
The keyword used to check a condition in JavaScript is if. It allows you to execute a block of code if a specified condition evaluates to true. The if statement is fundamental for controlling the flow of your JavaScript program.
How can you remove an HTML element using JavaScript?
- remove()
- hide()
- deleteElement()
- innerHTML = ''
You can remove an HTML element using the remove() method in JavaScript. For example, if you have an element with the ID elementToRemove, you can remove it using document.getElementById('elementToRemove').remove();. This method completely removes the element from the DOM (Document Object Model).
The comparison operator _______ checks for inequality, considering type coercion.
- ==
- ===
- !=
- !==
The comparison operator '!==' checks for inequality while also considering type coercion. It returns true if the values are not equal or if their types are not the same. For example, 5 !== "5" would evaluate to true because the number 5 is not equal to the string "5."
How does JavaScript implement inheritance internally?
- Through classes and interfaces
- Using a prototype chain and object delegation
- By creating abstract base classes
- Through function overloading
JavaScript implements inheritance through a prototype chain and object delegation. Each object has a prototype object, and if a property or method is not found on the object itself, it's looked up on the prototype chain. This mechanism allows objects to inherit properties and methods from their prototypes, facilitating code reuse and object-oriented programming in JavaScript. Classes and interfaces are not directly related to inheritance in JavaScript.
What is the primary difference between for...in and for...of loops?
- The order of iteration and syntax
- The type of objects they iterate over
- They are interchangeable
- The number of iterations they perform
The primary difference between for...in and for...of loops is in their order of iteration and syntax. For...in is used for iterating over object properties, while for...of is used for iterating over the values of iterable objects, like arrays and strings, and maintains the order.
Which of the following is a way to create a singleton object in JavaScript?
- Using the Factory Pattern
- Using the Prototype Pattern
- Using the Singleton Pattern
- Using the Module Pattern
A singleton object in JavaScript is typically created using the Module Pattern. The Module Pattern allows you to encapsulate code and create a single instance of an object, ensuring that only one instance of the object exists throughout your application. The other options (Factory, Prototype, and Singleton patterns) serve different purposes.
What was the main reason for JavaScript's creation at Netscape?
- To compete with Microsoft's JScript
- To create a replacement for HTML
- To enhance interactivity in web browsers
- To provide a server-side scripting language
JavaScript was primarily created to enhance interactivity in web browsers. It allowed developers to manipulate web page elements and respond to user actions, making websites more dynamic. While JavaScript later found uses on the server-side as well, its initial purpose was to enhance the client-side user experience.
The for...in loop will also iterate over the _______ properties of an object.
- Enumerable
- Non-enumerable
- Private
- Prototype
The for...in loop will also iterate over the enumerable properties of an object. Enumerable properties are those that can be iterated over using for...in, typically the object's own properties. Non-enumerable properties, private properties, and prototype properties are not included in this iteration.