0
0
Javascriptprogramming~10 mins

Importing values in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Importing values
Start
Import statement encountered
Look for exported values in module
Retrieve requested values
Assign values to local variables
Use imported values in code
End
When JavaScript sees an import statement, it finds the exported values from another file and assigns them to local variables to use in the current file.
Execution Sample
Javascript
import { greet } from './greetings.js';

console.log(greet('Alice'));
This code imports the 'greet' function from another file and uses it to print a greeting for 'Alice'.
Execution Table
StepActionEvaluationResult
1Encounter import statementimport { greet } from './greetings.js'Prepare to load 'greet' from greetings.js
2Load greetings.js moduleModule exports: { greet: function }Module loaded with 'greet' function
3Assign 'greet' to local variablegreet = function(name) { return `Hello, ${name}!`; }Local 'greet' ready to use
4Call greet('Alice')greet('Alice')'Hello, Alice!'
5console.log outputconsole.log('Hello, Alice!')Prints: Hello, Alice!
💡 All import values assigned and used; program ends.
Variable Tracker
VariableStartAfter importAfter function callFinal
greetundefinedfunction greet(name)function greet(name)function greet(name)
outputundefinedundefined'Hello, Alice!''Hello, Alice!'
Key Moments - 3 Insights
Why do we need to import 'greet' before using it?
Because 'greet' is defined in another file, the import statement brings it into the current file as shown in execution_table step 3.
What happens if the file path in import is wrong?
The module cannot be loaded (step 2), causing an error and stopping the program before assigning 'greet'.
Is 'greet' available before the import statement runs?
No, 'greet' is undefined before import (variable_tracker start), only assigned after loading the module (step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'greet' after step 3?
Aa function that takes a name and returns a greeting
Bundefined
Ca string 'Hello, Alice!'
Dnull
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 3 in the execution_table.
At which step does the program print 'Hello, Alice!'?
AStep 2
BStep 5
CStep 4
DStep 3
💡 Hint
Look for the console.log output in the execution_table.
If the import path './greetings.js' was incorrect, what would happen?
AThe program would assign 'greet' as undefined and continue
BThe program would print 'Hello, Alice!' anyway
CThe program would throw an error at step 2 and stop
DThe program would skip the import and use a default greet
💡 Hint
Refer to key_moments about what happens if the file path is wrong.
Concept Snapshot
Importing values in JavaScript:
- Use 'import { name } from "file";' to bring exported values.
- The imported names must match exported names.
- Imported values become local variables.
- Use them like normal variables after import.
- Errors occur if file path or names are wrong.
Full Transcript
This visual trace shows how JavaScript imports values from another file. First, the import statement is read. Then the module file is loaded, and the requested exported values are found. These values are assigned to local variables in the current file. After that, the imported values can be used like normal variables. For example, a function 'greet' is imported and called to print a greeting. If the file path is wrong, the program stops with an error. Variables like 'greet' start undefined and get assigned after import. This step-by-step helps beginners see how import works in practice.