Which comparison operator performs type coercion if the operands are of different types?
- == (Equality)
- === (Strict Equality)
- != (Inequality)
- !== (Strict Inequality)
The comparison operator == (Equality) performs type coercion if the operands are of different types. This means JavaScript will attempt to convert the operands to the same type before making the comparison. In contrast, === (Strict Equality) checks both value and type without coercion.
In what year was JavaScript introduced to the world?
- 1990
- 1995
- 2000
- 2005
JavaScript was introduced to the world in 1995. It was first released as part of Netscape Navigator 2.0. This event marked the beginning of JavaScript's journey as a popular and widely used scripting language for web development.
Considering JavaScript's type coercion, what will be the result of [] == ![]?
- TRUE
- FALSE
- undefined
- Throws an error
JavaScript performs type coercion when comparing values. In this case, the empty array [] is truthy, and ![] evaluates to false. When comparing, the empty array is coerced to false, resulting in true. This behavior can be surprising and is a common gotcha in JavaScript.
Which object is heavily used in AJAX to interact with server-side data?
- XMLHttpRequest
- JSON
- DOM
- Console
XMLHttpRequest is a crucial object used in AJAX (Asynchronous JavaScript and XML) to make HTTP requests to the server and interact with server-side data without requiring a page reload. It allows for asynchronous communication with the server, making web applications more dynamic and responsive.
How does the pop() method behave when applied on an empty array?
- It returns undefined and does nothing
- It throws an error
- It removes the last undefined element and returns it
- It returns null
When the pop() method is applied to an empty array in JavaScript, it returns undefined and does nothing to the array. There are no elements to remove, so it simply returns undefined without causing any errors.
In which scenario is a function expression preferred over a function declaration?
- When you need the function to be hoisted and accessible before its declaration in the code.
- When you want to declare a function as a named function to improve code readability and debugging.
- When you need to define a function inside an object or as a callback function in another function.
- When you want to declare a function as a global function for reuse across multiple scripts.
A function expression is preferred over a function declaration when you need to define a function inside an object, pass it as a callback function, or use it in a specific local scope. Function expressions are often used in scenarios where you want to create functions on the fly and keep them within a limited scope.
The break statement exits a while loop and continues executing the code that follows the loop at line number ________.
- immediately
- next
- specified
- labeled
The 'break' statement exits a while loop immediately and continues executing the code that follows the loop at the next line. It allows you to prematurely terminate a loop based on a certain condition, without completing the remaining iterations.
You're reviewing a pull request, and you see that a developer used var to declare a variable inside a for loop. You notice that the variable is being accessed outside the loop without any issues. Why is this possible?
- Variable Hoisting
- Block Scoping (let/const)
- Function Scoping
- Global Scope
This is possible due to "Variable Hoisting" in JavaScript. Variables declared with var are hoisted to the top of their containing function or global scope. This means that the variable is accessible anywhere within that scope, even before its actual declaration in the code.
You are building a single-page application, and you want to make an API call. You realize that you want to abort the fetch request if the user navigates away from the current page to avoid unnecessary data transfer. How can you achieve this?
- Use the fetch.abort() method to cancel the request when the user navigates away.
- Implement an if statement to check the navigation state and cancel the request accordingly.
- Set a timeout for the fetch request and cancel it if the user navigates away within the timeout period.
- There's no way to cancel a fetch request when the user navigates away.
To abort a fetch request when the user navigates away from the current page, you should implement an if statement that checks the navigation state and cancels the request accordingly. The other options are not suitable for this specific task, and there's no built-in fetch.abort() method in JavaScript.
An arrow function is defined using the _________ symbol.
- =>
- ->
- function
- =
An arrow function is defined using the => (fat arrow) symbol. This concise syntax is a shorthand for writing simple, one-liner functions in JavaScript. It's especially useful for functions that have no side effects and return a single expression. The => symbol distinguishes arrow functions from traditional function declarations.
Which of the following is NOT a state of a Promise?
- Pending
- Resolved
- Rejected
- Completed
In JavaScript, a Promise can be in one of three states: Pending (initial state), Resolved (fulfilled with a value), or Rejected (fulfilled with an error). "Completed" is not a valid state for a Promise; it's either resolved or rejected.
The method myArray.find(callback) returns _______ if no element passes the test.
- NaN
- undefined
- an empty array
- -1
The myArray.find(callback) method returns undefined if no element in the array passes the test provided by the callback function. This is because it signifies that no element satisfies the condition. If an element is found that passes the test, it returns that element.