JavaScript Modules - BasicsQuiz

Quiz 1 10 questions

Prerequisites for the quiz

  1. JavaScript Modules - Basics

Are you ready?

10 questions to solve

Instructions
  1. This quiz goes to full-screen once you press the Start button.
  2. At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
Is there a problem in the following <script> tag? Explain.
<script>
   import { greet } from './greeting.js';
   greet();
</script>
Indeed there is an issue in the <script> shown above. It contains module code — in particular, the import statement — which requires the script to have a type attribute set on it with the value "module". Hence, the correct choice is (B). For more info, refer to JavaScript Modules — Basics — Module scripts.
The type="module" attribute is required on a <script> tag if the file it links, via its src attribute, to contains module code. True or false?
Absolutely true. For one to be able to run module code, it's mandatory to have the type="module" attribute set on it. For more info, refer to JavaScript Modules — Basics — Module scripts.
Why does the following code throw an error?
export var y = 10;

x = 10;
JavaScript modules are in strict mode by default. Since it's invalid to assign to an undeclared variable in strict mode, the statement x = 10 above throws an error. For more info, refer to JavaScript Modules — Basics — Implicit strict mode.
What is the outcome of the main.js module below?
foo.js
var x = 10;

export function f() {
   console.log(x);
}
main.js
import { f } from './foo.js';

var x = 50;
f();
It logs 10 as it calls the function f() imported from the foo.js module which obviously refers to the x of that module. Hence, the correct choice is (C). For more info, refer to JavaScript Modules — Basics — Basic import and export.
The export keyword is correctly used in the following code. True or false? If false, then describe the issue.
var x;
export x = 10;
The export keyword can't be followed by an expression; only statements. Hence, the correct choice is (B). For more info, refer to JavaScript Modules — Basics — Basic import and export.
The export keyword is correctly used in the following code. True or false? If false, then describe the issue.
export var x;
x = 10;
The code shown above is perfectly valid. Hence, the correct choice is (A). For more info, refer to JavaScript Modules — Basics — Basic import and export.
The following two code snippets are equivalent to one another in what they export. True or false?

Snippet 1:

export var x = 10;

export function f() {
   console.log(x);
};

Snippet 2:

var x = 10;

function f() {
   console.log(x);
};

export { x, f };
Both the code snippets above export two names: x and f, and are, thus, equivalent to one another in what they export. This goes with choice (A). For more info, refer to JavaScript Modules — Basics — Combined named exports.
The default keyword can be used in an export statement either before the export keyword or after it. That is, it's a personal preference. True or false?
In an export statement, the default keyword can only come after the export keyword. Hence, the statement above is false and this goes with choice (B). For more info, refer to JavaScript Modules — Basics — Default import and export.
Identify the problem in the following code.
export const LEVEL = 1;

export var x = 10;
x = 20;

export default {
   id: 7,
   lang: 'en-US'
};

export default 'Hello World';
We can't have more than one default export in a module, which is what's happening above. Hence, the correct choice is (D). For more info, refer to JavaScript Modules — Basics — Default import and export.
What is the part as g in the following snippet referred to as?
import { f as g } from './utils.js';
It's called name aliasing — specifically, name aliasing an import. This goes with choice (A). For more info, refer to JavaScript Modules — Basics — Default import and export.