๐ Scenario: Imagine you are organizing a small party and need to greet each guest with the same message. Instead of writing the greeting again and again, you can use a function to say hello to each guest easily.
๐ฏ Goal: You will create a simple function to greet guests by name and then use it multiple times to show why functions help us avoid repeating code.
๐ What You'll Learn
Create a function called greet that takes one parameter called name
Inside the function, print the message Hello, [name]! using the parameter
Call the greet function three times with the names 'Alice', 'Bob', and 'Charlie'
Print the greetings to the console
๐ก Why This Matters
๐ Real World
Functions are used everywhere in programming to organize tasks and reuse code, like sending messages, calculating totals, or drawing shapes.
๐ผ Career
Understanding functions is essential for any programming job because they help write clean, efficient, and maintainable code.
Progress0 / 4 steps
1
Create a function called greet
Write a function named greet that takes one parameter called name. Inside the function, use console.log to print the message Hello, [name]! where [name] is the parameter value.
Javascript
Hint
Use the function keyword to create a function. Use backticks ` and ${} to insert the name inside the message.
2
Call the greet function with 'Alice'
Call the function greet with the argument 'Alice' to greet Alice.
Javascript
Hint
Call the function by writing its name followed by parentheses and the argument inside quotes.
3
Call the greet function with 'Bob' and 'Charlie'
Add two more calls to the function greet with the arguments 'Bob' and 'Charlie' to greet them as well.
Javascript
Hint
Call the function two more times with the new names inside quotes.
4
See the greetings printed
Run the program to see the greetings for Alice, Bob, and Charlie printed in the console.
Javascript
Hint
Look at the console output to see the greetings printed one after another.
Practice
(1/5)
1. Why do we use functions in JavaScript? function greet() { console.log('Hello!'); }
easy
A. To store data permanently
B. To make the program run faster
C. To reuse code and avoid repeating the same instructions
D. To create new variables automatically
Solution
Step 1: Understand the purpose of functions
Functions group code so it can be used multiple times without rewriting.
Step 2: Identify the benefit in the example
The greet function can be called many times to print 'Hello!' without repeating code.
Final Answer:
To reuse code and avoid repeating the same instructions -> Option C
Quick Check:
Functions help reuse code = C [OK]
Hint: Functions help reuse code easily [OK]
Common Mistakes:
Thinking functions make code run faster
Confusing functions with variables
Believing functions store data permanently
2. Which of the following is the correct way to define a function named sayHi in JavaScript?
easy
A. function: sayHi() { console.log('Hi'); }
B. def sayHi() { console.log('Hi'); }
C. func sayHi() { console.log('Hi'); }
D. function sayHi() { console.log('Hi'); }
Solution
Step 1: Recall JavaScript function syntax
JavaScript uses the keyword function followed by the name and parentheses.
Step 2: Check each option's syntax
Only function sayHi() { console.log('Hi'); } uses correct JavaScript syntax for defining a function.
Final Answer:
function sayHi() { console.log('Hi'); } -> Option D
Quick Check:
JavaScript functions start with 'function' keyword = D [OK]
Hint: JavaScript functions start with 'function' keyword [OK]
Common Mistakes:
Using Python or other language keywords like def or func
Adding colon after function keyword
Missing parentheses after function name
3. What will be the output of this code?
function add(a, b) {
return a + b;
}
console.log(add(3, 4));
medium
A. 7
B. 34
C. undefined
D. Error
Solution
Step 1: Understand the function behavior
The function add takes two numbers and returns their sum.
Step 2: Calculate the result of add(3, 4)
3 + 4 equals 7, so the function returns 7.
Final Answer:
7 -> Option A
Quick Check:
3 + 4 = 7 [OK]
Hint: Add numbers inside function to get sum [OK]
Common Mistakes:
Concatenating numbers as strings (getting 34)
Forgetting to return a value (getting undefined)
Syntax errors causing runtime error
4. Find the error in this function and choose the fix:
function greet() {
console.log('Hello!')
}
greet;
medium
A. Add a return statement inside greet
B. Change greet; to greet();
C. Remove the semicolon after console.log
D. Rename function to greetUser
Solution
Step 1: Identify how the function is called
The function is defined correctly but called as greet; which does not execute it.
Step 2: Correct the function call syntax
To run the function, parentheses are needed: greet();
Final Answer:
Change greet; to greet(); -> Option B
Quick Check:
Functions run with parentheses = B [OK]
Hint: Call functions with parentheses () [OK]
Common Mistakes:
Calling function without parentheses
Thinking return is needed to print
Changing function name unnecessarily
5. You want to write a program that prints a greeting message multiple times without repeating code. Which approach is best?
hard
A. Use a function to print the greeting, then call it multiple times
B. Write the greeting message many times in the code
C. Use variables to store the greeting but write console.log many times
D. Write the greeting inside a comment and copy it when needed
Solution
Step 1: Understand the problem of repeating code
Writing the same message many times makes code long and hard to change.
Step 2: Use functions to avoid repetition
Functions let you write the message once and call it many times, making code cleaner.
Final Answer:
Use a function to print the greeting, then call it multiple times -> Option A
Quick Check:
Functions help reuse code = A [OK]
Hint: Use functions to avoid repeating code [OK]
Common Mistakes:
Repeating code instead of using functions
Using comments instead of code
Misusing variables without functions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.