Why does 0.1 + 0.2 !== 0.3 in JavaScript?
- JavaScript has floating-point precision limitations
- 0.1 and 0.2 are not stored precisely in binary
- It's a bug in JavaScript
- JavaScript uses a different numeric system
In JavaScript, numbers are represented in binary, and not all decimal fractions can be precisely represented in binary. Due to this limitation, when you perform operations like addition with decimal numbers, you might encounter tiny rounding errors that make 0.1 + 0.2 !== 0.3. This behavior is not unique to JavaScript and occurs in many programming languages with floating-point arithmetic.
Given the short-circuiting nature of logical operators in JavaScript, what will be the output of the expression false && someUndeclaredVariable?
- FALSE
- TRUE
- Throws an error
- undefined
JavaScript's logical operators like && and
How does the await keyword manage the Promise’s resolve value?
- It changes the resolve value to a boolean.
- It extracts the resolve value from the Promise.
- It modifies the Promise's behavior.
- It cancels the Promise.
The 'await' keyword is used in async functions to pause the execution until the Promise is resolved. When the Promise resolves, the 'await' expression returns the resolved value. It doesn't change the value to a boolean or modify the Promise itself. This behavior is crucial for handling asynchronous operations more effectively.
You are tasked with refactoring a piece of legacy code where a function declaration within a conditional block is causing inconsistent behavior across different JavaScript engines. What is a potential solution to ensure consistent behavior?
- Use a function expression instead of a declaration within the conditional block.
- Ensure the function is declared with the 'let' keyword.
- Wrap the function declaration in a try-catch block to handle any errors.
- Split the code into multiple conditional blocks.
To ensure consistent behavior across different JavaScript engines, it's advisable to use a function expression within a conditional block instead of a function declaration. Function declarations are hoisted to the top of their containing function or script, which may lead to inconsistent results in legacy code.
In JavaScript, the "this" keyword inside an arrow function is defined by its _________ context.
- Global
- Lexical
- Local
- Execution
In JavaScript, the "this" keyword inside an arrow function is defined by its lexical context. Unlike regular functions, arrow functions do not have their own "this" binding, so they inherit the "this" value from the surrounding code block, which is determined by the lexical scope.
Which method can be used to stop the event from propagating in the capturing or bubbling phase?
- event.stopPropagation()
- event.preventDefault()
- event.stopPropagationPhase()
- event.cancelPropagation()
The event.stopPropagation() method is used to stop the event from propagating further in the DOM tree. It prevents both capturing and bubbling phases, ensuring that the event doesn't trigger any other event listeners on the same element or its ancestors. This can be helpful in controlling event flow.
In JavaScript, variables declared with the var keyword have _________ scope.
- Block
- Local
- Global
- Function
In JavaScript, variables declared with the var keyword have global scope. This means they are accessible throughout the entire function or script, regardless of where they are declared within that function or script. It's important to be cautious when using var to avoid unintended global variable declarations.
A ________ object represents a group of response headers, allowing you to query them and take different actions depending on the results.
- a) XMLHttpRequest Object
- b) FetchEvent Object
- c) Headers Object
- d) Worker Object
A Headers object represents a group of response headers. It allows you to query and manipulate these headers, which can be crucial when you need to inspect or modify headers in HTTP responses. You can use it for tasks like checking for specific headers or adding custom headers to a request.
In a code review, you spot the line const arr = [10, 20, 30]; followed by arr = [40, 50, 60];. What will be the outcome when this code is executed?
- It will result in an error.
- The arr variable will now reference [40, 50, 60].
- The original array [10, 20, 30] will be modified.
- It will create a new variable arr with [40, 50, 60].
The code will result in an error. When you declare a variable using const, it cannot be reassigned to a different value or reference. Attempting to reassign arr to [40, 50, 60] will throw a "TypeError" because it violates the immutability of const variables.
If you are developing a real-time application where any blocking of the event loop can lead to critical issues, how might you implement a "for" loop to process an array of data without introducing any blockage?
- Use for...of loop
- Use setInterval to break up iterations
- Use for...in loop
- Use a synchronous for loop with a delay
In a real-time application, using a for...of loop is the recommended approach because it doesn't block the event loop. It iterates through the array without causing delays. Using setInterval is not suitable for processing an array as it introduces an asynchronous behavior. for...in loop is used for object iteration, and a synchronous for loop with a delay would still block the event loop.