Which of the following is NOT a characteristic of closures in JavaScript?
- Data encapsulation and privacy
- Persistence of local variables
- Ability to access outer variables
- Limited use of memory resources
Closures in JavaScript are powerful because they allow functions to remember and access their outer (enclosing) variables even after the outer function has finished executing. This feature can lead to memory usage if not managed properly, but it's not a limitation or disadvantage of closures per se.
The ______ statement is used to specify a new condition to test if the first condition is false.
- else if
- else
- if else
- switch
The else if statement is used to specify a new condition to test if the first condition in an if statement is false. It allows for branching in code execution based on multiple conditions. It's a fundamental control structure in JavaScript.
You're debugging a piece of code that is returning an array in an unexpected order after a sort() method is applied. What could be a likely cause for this behavior given the default behavior of sort()?
- The array has mixed data types
- The sort() function is asynchronous
- The array elements are all numbers
- The array elements are strings
JavaScript's sort() method by default converts elements to strings and then compares their UTF-16 code units. This means that if the array contains mixed data types, the sorting order might be unexpected. For proper sorting, you should provide a compare function as an argument to sort().
A piece of code is unexpectedly returning -Infinity. What arithmetic operation could be causing this?
- Division (/)
- Addition (+)
- Exponentiation (**)
- Multiplication (*)
When a piece of code returns -Infinity, it is usually due to a division operation where you are dividing a finite number by zero. In JavaScript, dividing any finite number by zero results in -Infinity. Make sure to handle cases where division by zero can occur to prevent unexpected -Infinity values in your code.
When the interpreter encounters the following code var x = "5"; the typeof x will be _________.
- "string"
- "number"
- "boolean"
- "undefined"
In JavaScript, the typeof operator is used to determine the data type of a variable. When var x = "5"; is encountered, the value of x is a string because it is enclosed in double quotes. Therefore, typeof x will return "string". It's important to note that JavaScript is dynamically typed, meaning the type of a variable can change during runtime.
How can you add a method to an object in JavaScript?
- a) By using the Object.addMethod() method.
- b) By defining a function and assigning it as a property of the object.
- c) By using the Object.method() function.
- d) By using the object.method = function() syntax.
You can add a method to an object in JavaScript by defining a function and assigning it as a property of the object. For example, myObject.myMethod = function() { /* method code */ };. While you can use various patterns and techniques for method definition, there's no standard Object.addMethod() or Object.method() function.
How does hoisting behave in function declarations in JavaScript?
- Function declarations are moved to the top of their containing scope during compilation.
- Function declarations are not affected by hoisting.
- Hoisting only applies to variables, not functions.
- Function declarations are moved to the bottom of the code.
In JavaScript, hoisting is the mechanism by which variable and function declarations are moved to the top of their containing scope during compilation. This means that you can call a function declared with function before it appears in your code, and it will still work. However, it's important to note that only the declarations are hoisted, not the initializations. Understanding hoisting is crucial for writing clean and maintainable JavaScript code.
A _________ object is used to perform HTTP requests in AJAX.
- XMLHttpRequest
- JSON
- DOM
- Fetch
In AJAX (Asynchronous JavaScript and XML), the XMLHttpRequest object is used to perform HTTP requests asynchronously. It allows you to send and receive data from a server without refreshing the entire web page.
Which property of the event object is commonly used to prevent the default action of the event?
- event.stopPropagation()
- event.preventDefault()
- event.cancelBubble()
- event.halt()
To prevent the default action of an event in JavaScript, you commonly use the event.preventDefault() method. It stops the default behavior associated with the event, such as preventing a form from submitting or a link from navigating to a new page. This method is crucial for controlling the behavior of events.
You have an object containing user data and need to create an array of strings containing user details in a "key: value" format. Which loop might be most suitable for this task?
- for...in loop
- for...of loop
- while loop
- forEach() method
The for...of loop is most suitable for iterating over object properties when you want to create an array of strings. It directly iterates over iterable values like arrays and works well for this task by extracting key-value pairs from the object.