Salesforce Certified JavaScript Developer Practice Questions

Salesforce Certified JavaScript Developer exam preparation

Salesforce Certified JavaScript Developer practice questions in English: 180 questions with explanations. The mock exam has 60 questions and 105 minutes.

Salesforce Certified JavaScript Developer free question 4 / 10 · Domain 2: Objects, Functions, and Classes

A developer creates an instance before the class declaration appears in the file. try { new Widget(); } catch (err) { console.log(err.name); } class Widget {} What is logged?

  1. SyntaxError, because a class has to be declared before the first statement that mentions it
  2. TypeError, because the Widget name is hoisted as a function that is not callable with new
  3. The console stays empty, because class declarations are hoisted together with their body
  4. ReferenceError, because the class name is in its temporal dead zone until the declaration runs
Show the answer and explanation

Answer: D

Why this is correct
Unlike function declarations, class declarations have the same temporal dead zone restrictions as let or const. Using Widget before the declaration is evaluated throws a ReferenceError, which the catch block logs.

Why the other options are wrong
"TypeError, because the Widget name is hoisted as a function that is not callable with new" … Class declarations are not hoisted like function declarations. The name cannot be used before its declaration is evaluated.
"The console stays empty, because class declarations are hoisted together with their body" … The page says class declarations have the same temporal dead zone restrictions as let or const, so the early use throws.
"SyntaxError, because a class has to be declared before the first statement that mentions it" … The code parses without a problem. The error appears at run time when the name is read inside its temporal dead zone.

Key points
Declare a class before the first code that uses it. A function declaration can be called earlier, but a class cannot.

Go to the Salesforce Certified JavaScript Developer practice app (10 free questions; purchase unlocks all questions)

See the full practice set