0
0
Javascriptprogramming~5 mins

Importing values in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the import statement in JavaScript?
The import statement is used to bring functions, objects, or values from another file or module into the current file so you can use them.
Click to reveal answer
beginner
How do you import a single named value called <code>sum</code> from a module named <code>math.js</code>?
You write: <br><code>import { sum } from './math.js';</code><br>This imports only the <code>sum</code> function or value from the <code>math.js</code> file.
Click to reveal answer
intermediate
What is the difference between a default import and a named import?
A default import brings in the main exported value from a module without curly braces, like <code>import myFunc from './file.js';</code>. Named imports require curly braces and import specific exported values, like <code>import { myFunc } from './file.js';</code>.
Click to reveal answer
intermediate
How do you import all exported values from a module as a single object?
You use the syntax: <br><code>import * as utils from './utils.js';</code><br>This imports everything exported from <code>utils.js</code> as properties of the <code>utils</code> object.
Click to reveal answer
beginner
Can you import values from a module without specifying the file extension?
In modern JavaScript environments, you usually need to include the file extension like .js when importing modules, especially in browsers. Some tools like bundlers may allow omitting it.
Click to reveal answer
Which syntax correctly imports a default export named calculate from math.js?
Aimport calculate from './math.js';
Bimport { calculate } from './math.js';
Cimport * as calculate from './math.js';
Dimport './math.js';
How do you import only the named export multiply from math.js?
Aimport { multiply } from './math.js';
Bimport multiply from 'math.js';
Cimport * as multiply from './math.js';
Dimport multiply from './math.js';
What does this import statement do? import * as helpers from './helpers.js';
AImports nothing
BImports the default export as helpers
CImports only the named export helpers
DImports all exports as properties of the helpers object
Which is true about importing modules in JavaScript?
AYou can import values without specifying the file path.
BYou can import multiple named exports in one statement.
CYou must always use curly braces for default imports.
DYou cannot import from local files.
What happens if you try to import a named export that does not exist in the module?
AJavaScript throws an error at compile time.
BThe import works but the value is undefined.
CJavaScript throws an error at runtime.
DThe module exports all values by default.
Explain how to import a default export and a named export from the same module.
Think about how you write import statements with and without curly braces.
You got /3 concepts.
    Describe the difference between importing a single named export and importing all exports as an object.
    Consider the use of curly braces and the * as syntax.
    You got /3 concepts.