What will the following code snippet log to the console?
console.log(5 + '5');
Explanation:
JavaScript performs type coercion in this case. The number 5 is implicitly converted to a string, resulting in string concatenation rather than numerical addition.
Which of these is NOT a valid way to create a custom error in JavaScript?
Explanation:
While you can throw strings or objects as errors, 'CustomError' is not a built-in JavaScript error type. To create custom errors, you'd typically extend the base Error object.
Which event is fired when a user clicks on an HTML element?
Explanation:
The onclick
event is specifically triggered when an element is clicked.
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.
What is the purpose of event.preventDefault()
in form handling?
Explanation:
event.preventDefault()
prevents the default behavior of an event. In form handling, it stops the form's default submission action, allowing you to handle it using JavaScript.
What value is returned by confirm('Are you sure?')
if the user clicks the 'Cancel' button?
Explanation:
The confirm()
function returns a boolean value. If the user clicks 'OK', it returns true
; if they click 'Cancel', it returns false
. This allows you to branch your code based on the user's response.
Which event is commonly used for handling form submissions?
Explanation:
The 'submit' event is triggered when a form is submitted, allowing you to handle data validation or submission logic.
What is the difference between delete obj.property
and obj.property = undefined
?
Explanation:
delete
removes a property from an object. Setting a property to undefined
leaves the property in the object but with an undefined value.
How do you set the 'src' attribute of an image element with the ID 'profilePic' to 'new-image.jpg'?
Explanation:
While setAttribute
works, directly setting the src
property is a more concise and common approach for image elements in JavaScript.
How can you loop through the properties of an object myObject
?
Explanation:
The for...in
loop is used to iterate over the properties (keys) of an object.