Lesson: JavaScript Functions

What are Functions in JavaScript?

Functions are one of the core building blocks of JavaScript. They allow you to group code into reusable chunks. A function can take input (parameters), perform actions, and return output.

Functions help you avoid code repetition, and can make your code more readable and maintainable.

Here's a simple function example:

function greet(name) {
    return "Hello, " + name + "!";
}

let greetingMessage = greet("Alice");
console.log(greetingMessage);  // Output: "Hello, Alice!"
        

Interactive Demo: Try It Yourself!

Use the "Try It" editor below to experiment with JavaScript functions. Modify the code and click "Run" to see the result.


Quiz: Test Your Knowledge

1. What is the purpose of a function in JavaScript?




2. Which keyword is used to define a function in JavaScript?




3. How do you call a function in JavaScript?