Which keyword is used to declare a variable in JavaScript with block-level scope?
var
block
let
const
How do you set the 'src' attribute of an image element with the ID 'profilePic' to 'new-image.jpg'?
document.querySelector('#profilePic').src = 'new-image.jpg';
document.querySelector('#profilePic').setAttribute('src', '/new-image.jpg');
document.getElementById('profilePic').setAttribute('src', 'new-image.jpg');
document.getElementById('profilePic').src = 'new-image.jpg';
How do you prevent the default behavior of a form submission (e.g., page reload)?
event.stopPropagation();
return false;
event.cancelBubble = true;
event.preventDefault();
Which of the following is a valid way to define an arrow function that takes one parameter and returns its square?
const square = (x) => { x * x };
function square(x) { return x * x; }
let square = function(x) { return x * x }
const square = x => x * x;
What is the primary role of the browser's JavaScript engine?
Fetching data from a server
Interpreting and executing JavaScript code
Defining the structure of a web page
Styling web page elements
What will the following code snippet log to the console: console.log((function(x) { return x * 2; })(5));
console.log((function(x) { return x * 2; })(5));
5
10
undefined
ReferenceError
Which of these is a valid way to embed JavaScript in an HTML file?
<js> alert('Hello!'); </js>
<javascript> alert('Hello!'); </javascript>
How can you loop through the properties of an object myObject?
myObject
for (let i = 0; i < myObject.length; i++) {...}
while (myObject.hasNext()) {...}
for (let key in myObject) {...}
myObject.forEach(property => {...})
What does the querySelector() method return when it finds multiple matching elements in the DOM?
querySelector()
The last matching element
null
The first matching element
An array of all matching elements
What is the difference between delete obj.property and obj.property = undefined?
delete obj.property
obj.property = undefined
delete sets the property to null, while setting it to undefined removes it entirely.
delete
delete completely removes the property, while setting it to undefined keeps the property but with no value.
You cannot set a property to undefined in JavaScript.
They are functionally the same.