0
0
Javascriptprogramming~15 mins

Why functions are needed in Javascript - See It in Action

Choose your learning style9 modes available
Why functions are needed
πŸ“– 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
Need a 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
Need a 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
Need a 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
Need a hint?

Look at the console output to see the greetings printed one after another.