JavaScript Basic Error Types (SyntaxError, TypeError) Quiz

JavaScript
0 Passed
0% acceptance

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.

40 Questions
~80 minutes
1

Question 1

What is a SyntaxError in JavaScript?

A
An error thrown when code cannot be parsed according to language grammar
B
Any error caused by network failures
C
A runtime type mismatch
D
An unused variable warning
2

Question 2

Which situation commonly triggers a SyntaxError?

A
Forgetting a closing parenthesis or bracket
B
Dividing by zero
C
Accessing a property on undefined
D
Calling a function with the wrong arguments
3

Question 3

What does the following code throw?

javascript
function greet(
  console.log('hi')
}
A
SyntaxError
B
TypeError
C
ReferenceError
D
No error
4

Question 4

How does the JS engine decide to throw a SyntaxError?

A
During parsing when tokens violate grammar rules
B
Only after code executes in the browser
C
Whenever a promise rejects
D
When the call stack exceeds its limit
5

Question 5

Which tool helps prevent SyntaxErrors before running code?

A
A linting and formatting pipeline
B
A production error boundary
C
Service worker caching
D
setTimeout
6

Question 6

Why does using a reserved word as a variable name cause a SyntaxError?

A
Because reserved words have fixed grammatical meaning in the language
B
Because the variable is undefined
C
Because the variable is const
D
Because the code runs slowly
7

Question 7

Which practice reduces SyntaxErrors when writing large objects?

A
Trailing commas and consistent indentation
B
Relying on eval
C
Inlining all strings
D
Disabling formatting tools
8

Question 8

What error occurs when you run this snippet?

javascript
const data = JSON.parse('{ name: "Pat" }')
A
SyntaxError
B
TypeError
C
ReferenceError
D
RangeError
9

Question 9

Identify the outcome:

javascript
let x = 0
if (x === 0)
  console.log('zero')
  console.log('done')
A
SyntaxError due to ambiguous block
B
Only the first log runs
C
Both logs conditionally run
D
TypeError
10

Question 10

Which preventive step best avoids SyntaxErrors in template literals?

A
Using ESLint rules that flag unmatched ${ } sequences
B
Disabling template literals
C
Switching to string concatenation only
D
Wrapping every literal in eval
11

Question 11

What is a TypeError?

A
An error thrown when an operation is performed on the wrong kind of value
B
A syntax-level parsing failure
C
A networking timeout
D
A CSS validation issue
12

Question 12

What happens when this code executes?

javascript
const tools = undefined
tools.forEach(t => console.log(t))
A
TypeError: Cannot read properties of undefined
B
SyntaxError
C
No output but no error
D
ReferenceError
13

Question 13

Which scenario commonly causes a TypeError involving null?

A
Accessing a property on null without checking its value
B
Declaring a const variable
C
Writing a template literal
D
Importing a module twice
14

Question 14

What is a good strategy to prevent undefined vs null access errors?

A
Use optional chaining or early return guards before property access
B
Always convert everything to strings
C
Disable strict mode
D
Rely on implicit globals
15

Question 15

Which TypeError occurs when calling something that is not a function?

A
"is not a function"
B
"Unexpected identifier"
C
"Unexpected token"
D
"Illegal break"
16

Question 16

How does strict mode affect assignment to undeclared variables?

A
It throws a ReferenceError instead of creating an implicit global
B
It logs a warning only
C
It silently creates a var
D
It converts the value to string
17

Question 17

What happens in strict mode here?

javascript
'use strict'
function demo() {
  sloppyVar = 10
}
demo()
A
ReferenceError: sloppyVar is not defined
B
TypeError
C
SyntaxError
D
No error
18

Question 18

Which strict mode rule helps prevent silent TypeErrors?

A
Disallowing duplicate parameter names
B
Requiring semicolons
C
Forcing camelCase variable names
D
Blocking template literals
19

Question 19

Evaluate the outcome:

javascript
const settings = null
console.log(settings.theme)
A
TypeError: Cannot read properties of null
B
SyntaxError
C
Logs undefined
D
ReferenceError
20

Question 20

Which TypeError stems from invalid function invocation?

A
Calling array.length() as if it were a function
B
Using JSON.parse
C
Writing a for loop
D
Declaring const
21

Question 21

What does an error stack trace provide?

A
The call sequence leading to the error
B
Browser cache contents
C
Network latency
D
Package versions
22

Question 22

What does this snippet output?

javascript
try {
  throw new TypeError('Bad input')
} catch (err) {
  console.log(err instanceof TypeError)
}
A
true
B
false
C
TypeError
D
SyntaxError
23

Question 23

What is a good reason to rethrow an error inside catch?

A
To surface the issue after adding context or logging
B
To silence all errors globally
C
To convert TypeErrors into SyntaxErrors
D
To warm up the JIT compiler
24

Question 24

Analyze the behavior:

javascript
try {
  JSON.parse('{ invalid }')
} catch (err) {
  console.error('Failed:', err.message)
} finally {
  console.log('Cleanup')
}
A
Logs the error message and then "Cleanup"
B
Throws unhandled SyntaxError
C
Skips finally
D
Never executes catch
25

Question 25

How can you create a custom error message for a validation failure?

javascript
class ValidationError extends Error {
  constructor(field) {
    super(`${field} is required`)
    this.name = 'ValidationError'
  }
}
throw new ValidationError('email')
A
By subclassing Error and supplying a message in super
B
By overriding JSON.stringify
C
By editing console.log
D
By disabling strict mode
26

Question 26

Which error occurs when reading property "length" on a number?

A
Undefined returns undefined; no error
B
TypeError if you try to assign length
C
SyntaxError
D
RangeError
27

Question 27

What does this log?

javascript
const count = 5
count()
console.log('done')
A
TypeError: count is not a function
B
5
C
done
D
SyntaxError
28

Question 28

How can optional chaining reduce TypeErrors?

A
By returning undefined instead of throwing when accessing deep properties
B
By converting all values to strings
C
By disabling strict mode
D
By forcing synchronous code
29

Question 29

Which best practice avoids property access on non-objects?

A
Validate inputs and bail early if they are not objects
B
Rely on implicit coercion to convert types
C
Mutate built-in prototypes
D
Catch all errors and ignore them
30

Question 30

Why is property access on undefined a TypeError rather than SyntaxError?

javascript
function printUser(user) {
  console.log(user.name)
}
printUser(undefined)
A
Because the syntax is valid but the operation fails at runtime
B
Because undefined is a reserved word
C
Because undefined values cannot be declared
D
Because TypeError rules execute before parsing
31

Question 31

When is try/catch most appropriate?

A
When you expect a block of code may throw and you need to recover gracefully
B
For every function regardless of behavior
C
To replace validation logic
D
Only inside event handlers
32

Question 32

How can custom error messages aid debugging?

A
They tell future developers exactly what went wrong and how to fix it
B
They reduce bundle size
C
They eliminate the need for console logging
D
They convert SyntaxErrors into TypeErrors
33

Question 33

Which try/catch pattern is considered an anti-pattern?

javascript
try {
  // thousands of lines of unrelated logic
} catch (err) {
  // ignore all errors
}
A
Wrapping massive code blocks and catching all errors without handling them
B
Catching specific known failures
C
Logging and rethrowing
D
Using finally for cleanup
34

Question 34

How does using finally help when handling TypeErrors?

A
It guarantees cleanup steps run regardless of success or failure
B
It prevents TypeErrors from being thrown
C
It converts TypeErrors to SyntaxErrors
D
It only executes if no error occurs
35

Question 35

Why should custom errors extend Error instead of plain objects?

A
Extending Error preserves stack traces and instanceof checks
B
Plain objects cannot be thrown
C
Plain objects are prohibited in strict mode
D
Extending Error shortens bundle size
36

Question 36

How can source maps help when debugging SyntaxErrors in minified code?

A
They map minified positions back to original files and line numbers
B
They automatically fix the error
C
They disable minification
D
They wrap code in try/catch
37

Question 37

What benefit does enabling Break on Exceptions in dev tools provide?

A
It pauses execution exactly where the error occurs
B
It rewrites the code to avoid errors
C
It minifies scripts further
D
It forces strict mode
38

Question 38

How can logging err.stack be useful when sharing bug reports?

A
It provides call-site context for remote teammates
B
It redacts sensitive data automatically
C
It prevents the error
D
It speeds up the runtime
39

Question 39

Which statement about window.onerror is accurate?

A
It captures uncaught errors, enabling centralized logging
B
It prevents SyntaxErrors during parsing
C
It only runs on mobile browsers
D
It replaces try/catch entirely
40

Question 40

What best practice helps align SyntaxError and TypeError handling?

A
Combine automated tests, linting, and descriptive runtime guards
B
Disable all tooling to avoid slowdowns
C
Swallow every error silently
D
Use eval to dynamically rewrite code

QUIZZES IN JavaScript