In JavaScript, what does it mean for a function to have a closure?
Explanation:
Closures are a powerful feature where inner functions retain a memory of their lexical environment. This means they can still use variables from the scope where they were defined, even if the outer function is no longer active.
What will be logged to the console?
function outer() {
let x = 5;
function inner() {
console.log(x);
}
return inner;
}
const myFunc = outer();
myFunc();
Explanation:
This example illustrates closures. myFunc
holds a reference to the inner
function. Even though outer
has finished, inner
still has access to the x
variable from its surrounding scope.
Which statement can be used to exit a 'switch' statement?
Explanation:
The 'break' statement is crucial in a 'switch' statement. Without it, the code execution would fall through to the next case even if the current case matches. 'break' ensures that only the matching case's code is executed.
How many times will the following loop iterate?
for (let i = 1; i <= 10; i += 2) {
console.log(i);
}
Explanation:
The loop starts with 'i' as 1 and continues as long as 'i' is less than or equal to 10. In each iteration, 'i' is incremented by 2. So the loop will iterate 5 times with 'i' values: 1, 3, 5, 7, 9.
What does the Array.isArray()
method return when passed an object?
Explanation:
Array.isArray()
specifically checks if a value is an array. It returns false
for other data types, including objects.
What is the correct way to add an element 'apple' to the end of the array fruits = ['banana', 'orange']
?
Explanation:
The push()
method adds an element to the end of an array in JavaScript.
How do you access the value associated with the key 'name' in the object person = {name: 'John', age: 30}
?
Explanation:
You can access object properties using dot notation (object.property) or bracket notation (object['property']).
Which keyword is used to declare a variable whose value should not change after its initial assignment?
Explanation:
The const
keyword is used to declare constants. Once a variable is declared with const
, its value cannot be reassigned. However, it's important to note that const
does not make objects themselves immutable, just the reference to them.
How do you access the value entered into a text input field in a form?
Explanation:
The value
property of an input element (like a text field) stores the current value entered by the user.
What is the relationship between HTML, CSS, and JavaScript in web development?
Explanation:
This accurately describes the common roles of the three core web technologies. HTML forms the basic structure, CSS handles the visual presentation, and JavaScript provides interactivity and dynamic functionality.
What will the browser display after running this JavaScript code: alert('Hello') + ' World!';
?
Explanation:
The alert()
function pauses execution and displays a dialog box with the provided message ('Hello'). The following string concatenation (' World!') is never reached because the code execution is halted by the alert()
.
Is it possible to have multiple 'catch' blocks associated with a single 'try' block?
Explanation:
You can chain multiple catch
blocks to handle different error types differently. This allows for more precise error management.
Which keyword is used to define an arrow function in JavaScript?
Explanation:
The arrow (=>
) is used to define arrow functions, a concise syntax for writing function expressions.
How can you access the error message from a caught error?
Explanation:
Caught errors in JavaScript are typically Error objects. The error.message
property provides a descriptive message about the error.
Which data type represents a sequence of characters in JavaScript?
Explanation:
In JavaScript, the 'string' data type is specifically designed to store and manipulate textual data, representing sequences of characters.