Nearby lessons

27 of 40

🧠 JavaScript Function - Introduction

📌 What are JavaScript Functions?

In JavaScript, a function is a reusable block of code designed to perform a particular task. 🎯

Functions help organize code, reduce repetition, and make debugging easier. They are essential in any JavaScript application.

💡 Why Use Functions?

  • ✅ Reusability - Write once, use many times
  • ✅ Maintainability - Easier to update and fix
  • ✅ Readability - Code becomes more structured and clean
  • ✅ Debugging - Isolate logic for easier testing

🔧 Function Example

Here's a simple example showing how functions work:

Example03
Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧩 Function Without Argument

This type of function does not take any input. It performs a fixed action every time it's called.

Example04
Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧩 Function With Argument

This function accepts one or more values (arguments) as input and uses them inside the block.

Example05
Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧩 Function With Default Argument

JavaScript allows you to assign default values to parameters. If no value is passed, the default is used.

Example06
Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧩 Function With Return Value

This function processes input and returns a result using the return statement.

Example07
Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4