Bird
Raised Fist0
Javascriptprogramming~10 mins

Function expression in Javascript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Function expression
Start
↓
Define function expression
↓
Assign to variable
↓
Call function via variable
↓
Execute function body
↓
Return result
↓
End
This flow shows how a function expression is defined, assigned to a variable, then called to run its code and return a result.
Execution Sample
Javascript
const greet = function(name) {
  return `Hello, ${name}!`;
};

console.log(greet('Alice'));
Defines a function expression assigned to 'greet' that returns a greeting, then calls it with 'Alice' and prints the result.
Execution Table
StepActionEvaluationResult
1Define function expression and assign to 'greet'function(name) { return `Hello, ${name}!`; }Variable 'greet' holds the function
2Call 'greet' with argument 'Alice'greet('Alice')Function body runs with name='Alice'
3Return greeting string`Hello, Alice!`Returned value is 'Hello, Alice!'
4console.log prints returned valueconsole.log('Hello, Alice!')Output: Hello, Alice!
💡 Function call completes and program ends after printing greeting
Variable Tracker
VariableStartAfter Step 1After Step 2Final
greetundefinedfunction expression assignedfunction expression unchangedfunction expression unchanged
nameundefinedundefined'Alice'undefined (after function ends)
return valueundefinedundefinedundefined'Hello, Alice!'
Key Moments - 3 Insights
Why do we assign the function to a variable instead of naming it directly?
Because in a function expression, the function is anonymous and stored in the variable (see Step 1 in execution_table). This lets us call it later using that variable.
What happens when we call greet('Alice')?
The function runs with 'name' set to 'Alice' (Step 2), then returns the greeting string (Step 3), which is printed (Step 4).
Is the function executed immediately when assigned?
No, the function expression is only stored in 'greet' at Step 1. It runs only when called at Step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is stored in 'greet' after Step 1?
AThe string 'Hello, Alice!'
BA function expression
Cundefined
DThe argument 'Alice'
💡 Hint
Check the 'Result' column in Step 1 of execution_table
At which step does the function actually run its code?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Evaluation' columns in execution_table
If we change the argument to 'Bob', what changes in variable_tracker?
AReturn value becomes undefined
B'greet' variable changes to 'Bob'
C'name' becomes 'Bob' at After Step 2
DNo variables change
💡 Hint
Check the 'name' variable values in variable_tracker
Concept Snapshot
Function expression syntax:
const variable = function(parameters) { body };

- Function is anonymous and stored in a variable
- Call function via variable name
- Runs only when called
- Returns value with return statement
Full Transcript
This visual trace shows how a function expression is created and used in JavaScript. First, a function without a name is assigned to a variable called 'greet'. This variable now holds the function. When we call greet('Alice'), the function runs with 'name' set to 'Alice'. It returns the string 'Hello, Alice!'. Finally, console.log prints this returned string. The function does not run when assigned, only when called. Variables change as the function runs, especially the parameter 'name' and the return value. This helps beginners see step-by-step how function expressions work.

Practice

(1/5)
1. What is a function expression in JavaScript?
easy
A. A function that runs automatically when the page loads
B. A way to store a function inside a variable
C. A function declared with the function keyword only
D. A special type of loop

Solution

  1. Step 1: Understand function expressions

    A function expression means assigning a function to a variable so it can be used later.
  2. Step 2: Compare options

    Only A way to store a function inside a variable describes storing a function inside a variable, which matches the definition of a function expression.
  3. Final Answer:

    A way to store a function inside a variable -> Option B
  4. Quick Check:

    Function expression = function stored in variable [OK]
Hint: Function expressions assign functions to variables [OK]
Common Mistakes:
  • Confusing function expressions with function declarations
  • Thinking function expressions run automatically
  • Mixing up functions with loops
2. Which of the following is the correct syntax for a function expression?
easy
A. var myFunc = function() { return 5
B. function myFunc() { return 5; }
C. let myFunc = function myFunc() { return 5
D. const myFunc = function() { return 5; };

Solution

  1. Step 1: Identify function expression syntax

    A function expression assigns an anonymous function to a variable using = function() { ... }.
  2. Step 2: Check options for correct syntax

    const myFunc = function() { return 5; }; uses const with an anonymous function and ends with a semicolon, which is correct syntax.
  3. Final Answer:

    const myFunc = function() { return 5; }; -> Option D
  4. Quick Check:

    Function expression syntax = variable = function() { } [OK]
Hint: Function expressions assign anonymous functions to variables [OK]
Common Mistakes:
  • Using function declaration syntax instead of expression
  • Missing semicolon at the end
  • Using named function inside expression incorrectly
3. What will be the output of this code?
const greet = function(name) { return `Hello, ${name}!`; };
console.log(greet('Alice'));
medium
A. greet Alice!
B. Hello, name!
C. Hello, Alice!
D. Error: greet is not a function

Solution

  1. Step 1: Understand the function expression

    The variable greet holds a function that returns a greeting string with the given name.
  2. Step 2: Evaluate the function call

    Calling greet('Alice') returns Hello, Alice! which is printed by console.log.
  3. Final Answer:

    Hello, Alice! -> Option C
  4. Quick Check:

    Function call with 'Alice' returns greeting [OK]
Hint: Function expressions return values like normal functions [OK]
Common Mistakes:
  • Confusing variable name with parameter
  • Expecting function name in output
  • Thinking function expression causes error
4. Find the error in this function expression:
const add = function(a, b) {
  return a + b
};
console.log(add(2, 3));
medium
A. No error, code runs correctly
B. Function expression must have a name
C. Cannot use const with function expressions
D. Missing semicolon after return statement

Solution

  1. Step 1: Check syntax of function expression

    The function is assigned to add using const, which is valid. The return statement is correct without semicolon (optional in JS).
  2. Step 2: Verify code execution

    Calling add(2, 3) returns 5 and prints it. No syntax or runtime errors occur.
  3. Final Answer:

    No error, code runs correctly -> Option A
  4. Quick Check:

    Valid function expression runs without error [OK]
Hint: Semicolons after return are optional in JavaScript [OK]
Common Mistakes:
  • Thinking function expressions must be named
  • Believing semicolon is mandatory after return
  • Assuming const cannot hold functions
5. You want to create a function expression that doubles a number and then immediately call it with 4. Which code does this correctly?
hard
A. const double = function(n) { return n * 2; }(4); console.log(double);
B. function(n) { return n * 2; }(4);
C. const double = (n) => n * 2; double(4);
D. const double = function(n) { return n * 2; }; double(4);

Solution

  1. Step 1: Understand immediate function call with expression

    To call a function expression immediately, you add parentheses after the function definition.
  2. Step 2: Analyze options

    const double = function(n) { return n * 2; }(4); console.log(double); defines a function expression and immediately calls it with (4), assigning the result to double. Then it logs the result. This matches the requirement.
  3. Final Answer:

    const double = function(n) { return n * 2; }(4); console.log(double); -> Option A
  4. Quick Check:

    Function expression called immediately with (4) [OK]
Hint: Add (args) right after function expression to call immediately [OK]
Common Mistakes:
  • Not calling the function immediately
  • Assigning function instead of result
  • Using arrow function without immediate call
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard