What if you could fix a bug once and see it fixed everywhere instantly?
Why Importing values in Javascript? - Purpose & Use Cases
Imagine you have many JavaScript files, each with useful functions or data. Without importing, you must copy and paste the same code everywhere you need it.
Copying code manually is slow and risky. If you fix a bug or update a value, you must change it in every file. This causes mistakes and wastes time.
Importing lets you write code once and reuse it by bringing values or functions into other files easily. This keeps your code clean, consistent, and easy to update.
export const pi = 3.14; // copy this in every file needing pi
import { pi } from './constants.js'; // use pi directly without copying
Importing values enables building modular, maintainable code where updates happen in one place and reflect everywhere.
Think of a recipe book: instead of rewriting the same recipe in every chapter, you just refer to it once. If the recipe changes, all chapters get the update automatically.
Manual copying causes errors and wastes time.
Importing shares values across files easily.
It keeps code organized and simple to maintain.
Practice
calculate from a file math.js?Solution
Step 1: Understand named imports syntax
Named exports must be imported using curly braces around the exact exported name.Step 2: Match syntax with example
The syntaximport { calculate } from './math.js';correctly imports the named exportcalculate.Final Answer:
import { calculate } from './math.js'; -> Option BQuick Check:
Named import uses curly braces [OK]
- Using default import syntax for named exports
- Forgetting curly braces around named imports
- Trying to access named export as property after import
utils.js?Solution
Step 1: Recall default import syntax
Default exports are imported without curly braces using any name you choose.Step 2: Identify correct syntax
import utils from './utils.js';correctly imports the default export.Final Answer:
import utils from './utils.js'; -> Option DQuick Check:
Default import no braces [OK]
- Using curly braces for default import
- Incorrect braces placement
- Using * as for default import
export const name = 'Alice'; in person.js, what will be the output of this code?import { name } from './person.js';
console.log(name);Solution
Step 1: Understand named export and import
The variablenameis exported as a named export and imported correctly with curly braces.Step 2: Check the logged value
Sincenameis 'Alice',console.log(name);outputs 'Alice'.Final Answer:
Alice -> Option AQuick Check:
Named import logs exported value [OK]
- Forgetting curly braces causing ReferenceError
- Expecting default import without braces
- Confusing undefined with missing import
import { sum } from './math.js'
console.log(sum(2, 3));Assuming
sum is a default export in math.js.Solution
Step 1: Identify export type
The problem statessumis a default export, so it should be imported without curly braces.Step 2: Check import syntax
Using curly braces imports named exports, soimport { sum }is incorrect for default export.Final Answer:
Using curly braces for default export import -> Option CQuick Check:
Default export no braces [OK]
- Confusing default and named imports
- Ignoring export type in import
- Assuming semicolon is mandatory for correctness
// colors.js
export const red = '#FF0000';
export const blue = '#0000FF';
export default 'colors';
// app.js
import colors, { red, green } from './colors.js';
console.log(colors, red, green);What will happen when running
app.js?Solution
Step 1: Analyze exports in colors.js
There is a default export 'colors' and named exports 'red' and 'blue'. No 'green' export exists.Step 2: Analyze import statement in app.js
It imports default ascolorsand namedred, butgreendoes not exist, causing a SyntaxError during module loading.Final Answer:
SyntaxError due to invalid import -> Option AQuick Check:
Missing named export causes import error [OK]
- Thinking missing named import becomes undefined
- Confusing default and named imports
- Expecting ReferenceError at runtime
Ready to prove it?
Pass the test and this pattern moves unknown
