Debug fifteen ES6 programs: SyntaxErrors in class bodies, super, private fields, generators and imports; runtime errors from calling classes without new, this before super, weak keys, new Symbol and non-iterables; and silent bugs with Map, Set, exhausted generators and spread class instances.
Question 1Easy
JavaScript refuses to run the code below. Which statement correctly describes the problem?
main.js
class Cart {
function add(item) {
return item;
}
}
ASyntaxError: class methods are written without the function keyword – add(item) { … }
BIt defines a method called add
CIt defines a static function
DReferenceError
Correct Answer: Option A
A class body contains method definitions, fields and static blocks – not function declarations.
JavaScript refuses to run the code below. Which statement correctly describes the problem?
main.js
import { readFile } from "node:fs/promises";
console.log(typeof readFile);
ASyntaxError: import statements are only allowed in ES modules – use .mjs / "type": "module" (or type="module" in the browser)
BIt prints function
CIt prints undefined
DReferenceError: readFile is not defined
Correct Answer: Option A
This file is run as a classic (CommonJS) script, where import declarations are a syntax error: "Cannot use import statement outside a module". In such scripts use require() or dynamic import().
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·