📖 Scenario: Imagine you are baking a cake. You have a list of steps to follow in order. In JavaScript, the computer also follows steps one by one. This project will help you see how JavaScript runs code from top to bottom.
🎯 Goal: You will write a simple JavaScript program with variables and functions to see how the code runs step by step. You will learn how JavaScript executes commands in order and how functions are called.
📋 What You'll Learn
Create variables with exact names and values
Create a function with a specific name
Call the function in the right place
Print output using console.log exactly as instructed
💡 Why This Matters
🌍 Real World
Understanding execution flow helps you write programs that do tasks in the right order, like following a recipe.
💼 Career
Every programmer needs to know how code runs step by step to debug and build working applications.
Progress0 / 4 steps
1
Create variables for ingredients
Create two variables called flour and sugar. Set flour to 2 and sugar to 1.
Javascript
Hint
Use let to create variables and assign the numbers 2 and 1.
2
Create a function to mix ingredients
Create a function called mixIngredients that returns the sum of flour and sugar.
Javascript
Hint
Define a function using the function keyword and return the sum of the two variables.
3
Call the function and store the result
Create a variable called batter and set it to the result of calling the function mixIngredients().
Javascript
Hint
Call the function by writing its name followed by parentheses and assign the result to batter.
4
Print the batter amount
Use console.log to print the value of the variable batter.
Javascript
Hint
Use console.log(batter); to show the value in the console.
Practice
(1/5)
1. What happens when JavaScript encounters a function declaration in the code?
easy
A. The function runs immediately.
B. The function is stored but not run until called.
C. The function is ignored.
D. The function causes an error.
Solution
Step 1: Understand function declaration behavior
When JavaScript sees a function declared, it saves it in memory but does not execute it immediately.
Step 2: Recognize when functions run
Functions only run when they are explicitly called later in the code.
Final Answer:
The function is stored but not run until called. -> Option B
Quick Check:
Functions run only when called = A [OK]
Hint: Functions run only when called, not on declaration [OK]
Common Mistakes:
Thinking functions run immediately on declaration
Confusing declaration with invocation
Assuming functions are ignored until called
2. Which of the following is the correct way to call a function named greet in JavaScript?
easy
A. greet();
B. greet;
C. call greet();
D. function greet();
Solution
Step 1: Identify function call syntax
In JavaScript, calling a function requires the function name followed by parentheses.
Step 2: Check each option
greet(); uses greet(); which correctly calls the function. Others are incorrect syntax.
Final Answer:
greet(); -> Option A
Quick Check:
Function call needs parentheses = A [OK]
Hint: Call functions with parentheses: name() [OK]