In JavaScript, what does it mean for a function to have a closure?
It can access its own parameters.
It can modify global variables.
It can be passed as an argument to other functions.
It can access variables from its surrounding scope, even after the outer function has finished executing.
Which keyword is used to declare a variable whose value should not change after its initial assignment?
static
let
var
const
What is the correct syntax to change the text content of an element with the ID 'myElement' to 'Hello World!'?
document.querySelector('#myElement').textContent = 'Hello World!';
document.querySelector('#myElement').innerHTML = 'Hello World!';
document.getElementById('myElement').textContent = 'Hello World!';
document.getElementById('myElement').innerText = 'Hello World!';
How do you prevent the default behavior of a form submission (e.g., page reload)?
return false;
event.stopPropagation();
event.cancelBubble = true;
event.preventDefault();
Which loop in JavaScript is best suited for iterating through the properties of an object?
do-while
for
for...in
while
Which event is fired when a user clicks on an HTML element?
onmousedown
onclick
onload
onmouseover
What is the difference between a function declaration and a function expression in JavaScript?
Function declarations are hoisted, while function expressions are not.
Function expressions are hoisted, while function declarations are not.
There is no difference; they are interchangeable.
Function declarations can only be anonymous.
What is the output of the following JavaScript code: prompt('Enter your name:', 'John Doe');?
prompt('Enter your name:', 'John Doe');
It prints 'Enter your name: John Doe' to the console.
It sets the user's name to 'John Doe' in the browser's local storage.
It creates an alert box with the message 'Enter your name: John Doe'.
It displays a dialog box with the message 'Enter your name:' and a default input value 'John Doe', returning the user's input as a string.
Which method is used to select an HTML element with a specific ID?
getElementbyId()
getElementById()
querySelectorAll()
querySelector()
What is the scope of a variable declared with 'var' inside a function?
Block scope
Global scope
Function scope
Lexical scope