What is the main difference between function declaration and function expression in JavaScript?
- Function declarations are hoisted, while function expressions are not.
- Function expressions can be named or anonymous, while function declarations must have a name.
- Function declarations are used for defining methods in objects, while function expressions are used for standalone functions.
- Function expressions are more efficient than function declarations.
The primary difference between function declaration and function expression in JavaScript is hoisting. Function declarations are hoisted, which means they are moved to the top of their containing scope during compilation. This allows you to call the function before it's declared in your code. Function expressions, on the other hand, are not hoisted, so they can only be used after their declaration in the code. Understanding this difference is crucial for managing the order of function calls in your JavaScript programs.
In a UI with nested dropdown menus, a developer wants to ensure that clicking a nested menu item does not trigger the click event of its parent menu. What method can be used to stop the event from reaching the parent menu?
- event.stopPropagation()
- event.preventDefault()
- event.stopImmediatePropagation()
- event.cancelBubble()
To prevent the event from reaching the parent menu when clicking a nested menu item, you can use the event.stopPropagation() method. This will stop the event from propagating up the DOM tree and prevent the parent menu's click event from being triggered. event.preventDefault() is used to prevent the default behavior of an event, not to stop event propagation.
When defining a method in an object, the function associated with a property is referred to as a _________.
- variable
- constructor
- function
- method
When defining a method in an object, the function associated with a property is referred to as a function. Methods are functions that are properties of objects. They allow you to perform actions or calculations using the data and context within the object. Understanding methods is fundamental in object-oriented programming with JavaScript.
When using a do-while loop, the loop will always execute at least ________ time(s).
- 0
- 1
- 2
- 3
When using a do-while loop, the loop will always execute at least '1' time. This is because the loop condition is checked after the loop body has executed, so it will execute at least once before checking the condition for subsequent iterations.
_________ is the organization that now oversees the ECMAScript specification, which serves as the basis for JavaScript.
- W3C
- Mozilla
- ECMA International
- ISO
ECMA International is the organization that now oversees the ECMAScript specification, which serves as the basis for JavaScript. ECMAScript is the standardized version of JavaScript that various browsers implement.
The shift() method will return _______ when it is used on an empty array.
- undefined
- NaN
- 0
- FALSE
The shift() method removes and returns the first element from an array. When used on an empty array, it returns undefined because there are no elements to remove. This behavior allows you to check if an array is empty by evaluating the result of shift().
How can you handle errors with Fetch API in a way that also catches HTTP error statuses?
- Using try...catch blocks
- Checking the status property
- Using the .then() method
- Using the .error() method
You can handle errors with Fetch API by using try...catch blocks. While the Fetch API does not throw exceptions for HTTP error statuses (e.g., 404 or 500), it does throw exceptions for network errors (e.g., no internet connection). By wrapping your Fetch code in a try...catch block, you can catch both types of errors and handle them appropriately, ensuring a robust error-handling mechanism.
The ________ loop is useful for iterating through the elements of an array.
- while
- for
- do...while
- forEach
The "for" loop is commonly used for iterating through the elements of an array in JavaScript. It provides more control over the iteration process, allowing you to specify the initialization, condition, and increment, making it suitable for looping through arrays by using the array's length as a condition. The other loop types mentioned are not as commonly used for this purpose.
document.querySelector('p') will select the _________
element in the HTML document.
- first
- last
- random
- first matching
document.querySelector('p') will select the first
element in the HTML document that matches the selector. If multiple
elements exist, it selects the first one it encounters in the document's order.
To merge two arrays into a single array, you can use the _______ method.
- merge()
- concat()
- join()
- combine()
The correct method is concat(). The concat() method is used to merge two or more arrays into a single array. It doesn't modify the original arrays but returns a new array containing the elements from the source arrays. For example, const mergedArray = array1.concat(array2); merges array1 and array2 into mergedArray.
How can a "for" loop be used to iterate through the properties of an object?
- By using for...of loop
- By using for...in loop
- By using forEach method
- By using while loop
To iterate through the properties of an object, you should use a for...in loop. This loop iterates over the enumerable properties of an object and allows you to access each property name (key). The for...of loop is used for iterating over iterable objects like arrays. The forEach method is specifically used for arrays, and while loops are generally used for repetitive tasks with a condition.
In order to make an object iterable with a for...of loop, you need to define its _______ method.
- loop
- forEach
- Symbol.iterator
- Object.iterable
To make an object iterable with a for...of loop, you need to define its Symbol.iterator method. This method should return an iterator object, which must have a next method that provides values one at a time. This enables you to use a for...of loop to iterate over the object's elements.