What is the output of the following JavaScript code: prompt('Enter your name:', 'John Doe');?
prompt('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.
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'.
How do you set the 'src' attribute of an image element with the ID 'profilePic' to 'new-image.jpg'?
document.querySelector('#profilePic').setAttribute('src', '/new-image.jpg');
document.getElementById('profilePic').src = 'new-image.jpg';
document.getElementById('profilePic').setAttribute('src', 'new-image.jpg');
document.querySelector('#profilePic').src = 'new-image.jpg';
What is the purpose of the 'catch' block in a try...catch statement?
To re-throw the caught error
To execute code only if an error doesn't occur
To handle the error thrown in the 'try' block
To specify the type of error to catch
Which method removes the first element from an array and returns it?
unshift()
shift()
pop()
slice()
What is the primary role of the browser's JavaScript engine?
Fetching data from a server
Defining the structure of a web page
Styling web page elements
Interpreting and executing JavaScript code
What is the difference between delete obj.property and obj.property = undefined?
delete obj.property
obj.property = undefined
You cannot set a property to undefined in JavaScript.
undefined
They are functionally the same.
delete sets the property to null, while setting it to undefined removes it entirely.
delete
null
delete completely removes the property, while setting it to undefined keeps the property but with no value.
What is the correct way to add an event listener that logs 'Button Clicked!' to the console when a button with the ID 'myButton' is clicked?
document.querySelector('#myButton').addEventListener('click', () => console.log('Button Clicked!'));
document.getElementById('myButton').addEventListener('click', console.log('Button Clicked!'));
document.getElementById('myButton').addEventListener('click', function() { console.log('Button Clicked!'); });
document.querySelector('#myButton').onclick = console.log('Button Clicked!');
What is the value of 'x' after the following code is executed?
let x = 5; if (x > 10) { x = 10; } else { x = 0; }
5
0
10
What does the Array.isArray() method return when passed an object?
Array.isArray()
true
"object"
object
false
Which loop in JavaScript is best suited for iterating through the properties of an object?
for...in
for
while
do-while