SparkJS Logo
HomeBeginnerFunctions

🎁 Functions

Master functions to write reusable and organized code

⏱️ 2.5 hours📚 9 sections⭐ Beginner

Your Progress

0 / 9 completed

📚 Lessons

🎁

What are Functions?

Functions are reusable blocks of code that perform specific tasks! Think of them as mini-programs inside your program. Instead of writing the same code over and over, you write it once in a function and use it whenever you need!

💻 Code Example:

// Without function - repetitive
console.log("Welcome, Player1!");
console.log("Welcome, Player2!");
console.log("Welcome, Player3!");

// With function - much better!
function greetPlayer(name) {
  console.log("Welcome, " + name + "!");
}

greetPlayer("Player1");
greetPlayer("Player2");
greetPlayer("Player3");

// Now you can greet anyone easily!
greetPlayer("John");
greetPlayer("Sarah");

💡 Tip: Copy this code and run it in your browser console to see it in action!

📝 Key Points:

  • Functions let you reuse code
  • Write once, use many times
  • Make your code organized and clean