JavaScript

Functions

Master JavaScript functions! Learn function declarations, expressions, arrow functions, parameters, and return values.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Master JavaScript functions! Learn function declarations, expressions, arrow functions, parameters, and return values. This hands-on tutorial focuses on practical implementation of functions concepts.

Functions

Functions are the building blocks of any application. They allow you to encapsulate code, reuse it, and make your programs modular.

1. What are Functions? 🎯

What is it? A block of code designed to perform a particular task. You define it once and can "call" (execute) it multiple times.

Why use it?

  • Reusability: Write code once, use it everywhere.
  • Organization: Break complex problems into smaller, manageable pieces.
  • DRY Principle: "Don't Repeat Yourself".

How it works:

  1. Define the function.
  2. Call the function by its name.
JAVASCRIPT PLAYGROUND
⏳ Loading editor…

2. Parameters & Arguments 🎁

Functions become powerful when they can accept data.

  • Parameters: Variables listed in the function definition (placeholders).
  • Arguments: Real values passed to the function when calling it.
JAVASCRIPT PLAYGROUND
⏳ Loading editor…

3. Return Values 🔙

A function doesn't just print things; it can send data back to the code that called it.

Crucial: The return statement immediately stops the function's execution.

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

4. Function Expressions & Arrow Functions 🏹

There are different ways to write functions in JavaScript.

Function Declaration (Traditional)

Hoisted (can be called before definition).

function sum(a, b) {
  return a + b;
}

Function Expression

Not hoisted (must be defined before use).

const sum = function(a, b) {
  return a + b;
};

Arrow Function (Modern ES6)

Concise syntax. Great for one-liners.

// Implicit return (no braces needed for single line)
const sum = (a, b) => a + b;

// Block body (needs braces and return)
const multiply = (a, b) => {
  const result = a * b;
  return result;
};
JAVASCRIPT PLAYGROUND
⏳ Loading editor…

5. Scope 🔭

Variables defined inside a function are local to that function. They cannot be accessed from outside.

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

Practical Example: Temperature Converter 🌡️

JAVASCRIPT PLAYGROUND
⏳ Loading editor…

AI Mentor

Confused about "JavaScript functions, parameters, return values, arrow functions, and scope"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 4

What keyword stops a function and sends a value back?

stop
break
return
send

Key Takeaways

Functions are reusable blocks of code.
Parameters are inputs, Return is output.
Arrow Functions (=>) are modern and concise.
Scope keeps variables safe inside functions.
DRY: Don't Repeat Yourself—use functions!

What's Next?

Congratulations! You've completed Module 1. Next, we start Module 2: Core JavaScript with Arrays!

Keep coding! 🚀