Challenge - 5 Problems
Default Export Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this default export import?
Consider two files:
module.js:
main.js:
What will be printed when
module.js:
export default function greet() { return 'Hello!'; } main.js:
import greet from './module.js';
console.log(greet());
What will be printed when
main.js runs?Javascript
export default function greet() { return 'Hello!'; } import greet from './module.js'; console.log(greet());
Attempts:
2 left
💡 Hint
Default exports can be imported with any name and used directly.
✗ Incorrect
The default export is a function named greet. Importing it as greet allows calling greet() which returns 'Hello!'.
❓ Predict Output
intermediate2:00remaining
What happens if you import a default export with curly braces?
Given
module.js:
and
main.js:
What will happen when running
module.js:
export default 42;
and
main.js:
import { default } from './module.js';
console.log(default);What will happen when running
main.js?Javascript
export default 42; import { default } from './module.js'; console.log(default);
Attempts:
2 left
💡 Hint
Default exports are imported without curly braces.
✗ Incorrect
You cannot import a default export using curly braces. This causes a syntax error.
🔧 Debug
advanced2:00remaining
Why does this default export import cause a runtime error?
Look at these files:
module.js:
main.js:
What error will occur when running
module.js:
const value = 100;
export default value;
main.js:
import val from './module.js';
console.log(val());
What error will occur when running
main.js and why?Javascript
const value = 100; export default value; import val from './module.js'; console.log(val());
Attempts:
2 left
💡 Hint
Check what val actually is and how it is used.
✗ Incorrect
The default export is a number 100, not a function. Calling val() causes a TypeError.
📝 Syntax
advanced2:00remaining
Which import statement correctly imports the default export?
Given
module.js:
Which import statement in
module.js:
export default class Person {}Which import statement in
main.js is correct?Javascript
export default class Person {}
Attempts:
2 left
💡 Hint
Default exports are imported without braces and without the keyword default.
✗ Incorrect
Option A correctly imports the default export as Person. Option A tries to import a named export which does not exist. Option A imports an object with all exports, not the default directly. Option A is invalid syntax.
🚀 Application
expert3:00remaining
What is the output of this combined default and named export import?
Consider
module.js:
main.js:
What will be printed when running
module.js:
export default function() { return 'default'; }
export const named = () => 'named';main.js:
import def, { named } from './module.js';
console.log(def());
console.log(named());What will be printed when running
main.js?Javascript
export default function() { return 'default'; } export const named = () => 'named'; import def, { named } from './module.js'; console.log(def()); console.log(named());
Attempts:
2 left
💡 Hint
Default and named exports can be imported together with correct syntax.
✗ Incorrect
The default export is an unnamed function returning 'default'. The named export is a function returning 'named'. Both are called and logged in order.