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:

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🧩 Function Without Argument

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

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🧩 Function With Argument

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

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🧩 Function With Default Argument

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

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🧩 Function With Return Value

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

Code Example
PREVIEW READY
Loading Editor...
Live Preview

🧠 Test Your Knowledge

4 Questions

Progress: 0 / 4
Keep Going!JS - Global & Local Variables