Login Account

JavaScript Questions

S
Saurabh Gupta
Score: 🔥 27
Beginner
Took 11 minutes
Programming Language
JavaScript
90%
Questions Attempted
10 / 10
Correct Answers
9
Incorrect Answers
1
Points Earned
🔥 9

Question 1

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.

Question 2

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.

Question 3

Which event is fired when a user clicks on an HTML element?

Explanation:

The onclick event is specifically triggered when an element is clicked.

Question 4

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.

Question 5

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.

Question 6

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.

Question 7

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.

Question 8

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.

Question 9

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.

Question 10

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.