Which method is commonly used to send data as JSON using Fetch API?
- fetch.post()
- fetch.data()
- fetch.sendJSON()
- fetch()
The fetch() method is commonly used to send data as JSON using the Fetch API. To send JSON data, you can create a request object and use the JSON.stringify() method to convert your data into a JSON string before sending it.
You are asked to create an object that should be instantiated only once and reused in other instances. Which design pattern would you implement?
- Singleton Pattern
- Factory Pattern
- Observer Pattern
- Prototype Pattern
In this scenario, you would implement the Singleton Pattern. The Singleton Pattern ensures that a class has only one instance and provides a global point of access to that instance. It's useful when you need to maintain a single instance of an object that is shared across multiple parts of your application. This can help in scenarios such as creating a global configuration or managing a resource pool.
The main advantage of using arrow functions comes from the lack of a new _________ binding.
- execution context
- lexical scope
- closure
- variable
The main advantage of using arrow functions is that they do not create a new execution context or this binding. Instead, they capture the this value from their surrounding lexical scope. This behavior is known as "lexical scoping," and it provides a predictable way to maintain the value of this in various situations. Thus, the correct option is execution context.
What is the result of the expression NaN == NaN in JavaScript?
- TRUE
- FALSE
- NaN
- Throws an error
In JavaScript, NaN is not equal to itself. So, the expression NaN == NaN evaluates to false. This behavior is because NaN represents a value that is "Not-a-Number," and it's treated as unordered and not equal to any other value, including itself.
What is the purpose of the "else" statement in JavaScript?
- To specify an alternate code block to execute when the condition is false
- To terminate the program
- To define a variable
- To loop through an array
The "else" statement in JavaScript serves the purpose of specifying an alternate code block to execute when the condition specified in the "if" statement is false. It provides an alternative path for your code's execution.
In JavaScript, the arithmetic operator _______ is used to exponentiate a number.
- **
- ^
- exp
- pow
In JavaScript, the ** operator is used to exponentiate a number. For example, 2 ** 3 evaluates to 8, as it calculates 2 raised to the power of 3. This operator was introduced in ECMAScript 2016 (ES6) to provide a concise way of performing exponentiation.
What is the purpose of the innerHTML property in JavaScript?
- To access the element's content
- To set the element's ID
- To toggle element visibility
- To retrieve the element's tag name
The innerHTML property in JavaScript is used to access the content of an HTML element. It allows you to retrieve or modify the HTML content within the element, including any child elements, text, or HTML tags.
To strictly compare the inequality of two operands without type coercion, use _______.
- ==
- ===
- !=
- !==
To strictly compare the inequality of two operands without type coercion in JavaScript, you should use the === operator. This operator checks both the value and the data type of the operands. In contrast, the == operator performs type coercion and may not provide the desired strict comparison.
What is the purpose of the capture parameter in addEventListener method?
- To specify the event phase
- To control event order
- To attach multiple listeners simultaneously
- To prevent event propagation during capture phase
The capture parameter in the addEventListener method is used to specify the event phase during which the listener should be triggered. When set to true, the listener is triggered during the capturing phase, and when set to false, it is triggered during the bubbling phase. This parameter helps control the order of event execution.
Which method can be used to insert an HTML element as the first child of a parent element?
- appendChild()
- insertBefore()
- prepend()
- createElement()
To insert an HTML element as the first child of a parent element, you should use the prepend() method. This method adds the element as the first child, making it the top element within the parent. appendChild() adds it as the last child. insertBefore() requires specifying a reference node. createElement() creates an element but doesn't insert it.