0
0
Javascriptprogramming~20 mins

Default exports in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Default Export Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this default export import?
Consider two files:

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());
Aundefined
BHello!
CTypeError: greet is not a function
DSyntaxError: Unexpected token export
Attempts:
2 left
💡 Hint
Default exports can be imported with any name and used directly.
Predict Output
intermediate
2:00remaining
What happens if you import a default export with curly braces?
Given

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);
ASyntaxError: Unexpected token default
Bundefined
C42
DReferenceError: default is not defined
Attempts:
2 left
💡 Hint
Default exports are imported without curly braces.
🔧 Debug
advanced
2:00remaining
Why does this default export import cause a runtime error?
Look at these files:

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());
ASyntaxError: Unexpected token import
B100
CReferenceError: val is not defined
DTypeError: val is not a function
Attempts:
2 left
💡 Hint
Check what val actually is and how it is used.
📝 Syntax
advanced
2:00remaining
Which import statement correctly imports the default export?
Given

module.js:
export default class Person {}

Which import statement in main.js is correct?
Javascript
export default class Person {}
Aimport Person from './module.js';
Bimport { Person } from './module.js';
Cimport * as Person from './module.js';
Dimport default Person from './module.js';
Attempts:
2 left
💡 Hint
Default exports are imported without braces and without the keyword default.
🚀 Application
expert
3:00remaining
What is the output of this combined default and named export import?
Consider

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());
A
named
default
BSyntaxError: Unexpected token import
C
default
named
DTypeError: def is not a function
Attempts:
2 left
💡 Hint
Default and named exports can be imported together with correct syntax.