JavaScript Basic Error Types (SyntaxError, TypeError) Quiz
Explore 40 JavaScript questions covering SyntaxError definitions, parsing mistakes, prevention strategies, TypeError causes, undefined versus null access issues, strict mode implications, debugging stacks, try/catch usage, and best practices for runtime resilience.
Question 1
What is a SyntaxError in JavaScript?
Question 2
Which situation commonly triggers a SyntaxError?
Question 3
What does the following code throw?
function greet(
console.log('hi')
}Question 4
How does the JS engine decide to throw a SyntaxError?
Question 5
Which tool helps prevent SyntaxErrors before running code?
Question 6
Why does using a reserved word as a variable name cause a SyntaxError?
Question 7
Which practice reduces SyntaxErrors when writing large objects?
Question 8
What error occurs when you run this snippet?
const data = JSON.parse('{ name: "Pat" }')Question 9
Identify the outcome:
let x = 0
if (x === 0)
console.log('zero')
console.log('done')Question 10
Which preventive step best avoids SyntaxErrors in template literals?
Question 11
What is a TypeError?
Question 12
What happens when this code executes?
const tools = undefined
tools.forEach(t => console.log(t))Question 13
Which scenario commonly causes a TypeError involving null?
Question 14
What is a good strategy to prevent undefined vs null access errors?
Question 15
Which TypeError occurs when calling something that is not a function?
Question 16
How does strict mode affect assignment to undeclared variables?
Question 17
What happens in strict mode here?
'use strict'
function demo() {
sloppyVar = 10
}
demo()Question 18
Which strict mode rule helps prevent silent TypeErrors?
Question 19
Evaluate the outcome:
const settings = null
console.log(settings.theme)Question 20
Which TypeError stems from invalid function invocation?
Question 21
What does an error stack trace provide?
Question 22
What does this snippet output?
try {
throw new TypeError('Bad input')
} catch (err) {
console.log(err instanceof TypeError)
}Question 23
What is a good reason to rethrow an error inside catch?
Question 24
Analyze the behavior:
try {
JSON.parse('{ invalid }')
} catch (err) {
console.error('Failed:', err.message)
} finally {
console.log('Cleanup')
}Question 25
How can you create a custom error message for a validation failure?
class ValidationError extends Error {
constructor(field) {
super(`${field} is required`)
this.name = 'ValidationError'
}
}
throw new ValidationError('email')Question 26
Which error occurs when reading property "length" on a number?
Question 27
What does this log?
const count = 5
count()
console.log('done')Question 28
How can optional chaining reduce TypeErrors?
Question 29
Which best practice avoids property access on non-objects?
Question 30
Why is property access on undefined a TypeError rather than SyntaxError?
function printUser(user) {
console.log(user.name)
}
printUser(undefined)Question 31
When is try/catch most appropriate?
Question 32
How can custom error messages aid debugging?
Question 33
Which try/catch pattern is considered an anti-pattern?
try {
// thousands of lines of unrelated logic
} catch (err) {
// ignore all errors
}Question 34
How does using finally help when handling TypeErrors?
Question 35
Why should custom errors extend Error instead of plain objects?
Question 36
How can source maps help when debugging SyntaxErrors in minified code?
Question 37
What benefit does enabling Break on Exceptions in dev tools provide?
Question 38
How can logging err.stack be useful when sharing bug reports?
Question 39
Which statement about window.onerror is accurate?
Question 40
What best practice helps align SyntaxError and TypeError handling?
