mc0de
JavaScript · Lesson 07 of 17

JavaScript Functions

Write code once and use it again! Discover how functions organize instructions, accept information, and return useful results to make your JavaScript cleaner and more powerful.

Reusable Code Parameters Return Values Arrow Functions
● ● ●   functions.js
function greet(name) {
  return "Hello, " + name + "!";
}

console.log(greet("Alex"));
CONSOLE OUTPUT
> Ready to greet…
01 / Getting Started

What Are JavaScript Functions?

Imagine you’re building a website that welcomes visitors. Every time someone signs in, you want to display a friendly greeting. You could write the same instructions in several places, but that quickly becomes repetitive.

A function groups instructions into a reusable block of code. You define it once, then call it whenever you need it.

Your first function
function sayHello() {
  console.log("Hello, developer!");
}

sayHello();
sayHello();
Console output
Hello, developer! Hello, developer!

The first part defines the function. The parentheses in sayHello() are how you call it. Each call runs the instructions inside the function.

Remember: Defining a function doesn’t automatically run its body. You must call it, unless some other code is set up to call it for you.
02 / Parameters & Arguments

Give Your Functions Information

A greeting that always says the same thing is useful, but what if you want to welcome different visitors? Functions can accept information through parameters.

Passing a name into a function
function greet(name) {
  console.log("Hello, " + name + "!");
}

greet("Alex");
greet("Jordan");
Console output
Hello, Alex! Hello, Jordan!

name is a parameter: the placeholder in the function definition. "Alex" and "Jordan" are arguments: the actual values supplied when calling the function.

You can add multiple parameters by separating them with commas.

Two parameters
function introduce(name, city) {
  console.log(name + " lives in " + city);
}

introduce("Alex", "Irvine");
03 / Return Values

Get a Result Back From a Function

Sometimes you don’t want a function to display a message immediately. You want it to calculate or create something that other parts of your program can use.

The return statement sends a result back to the code that called the function.

Returning a calculation
function addNumbers(a, b) {
  return a + b;
}

const total = addNumbers(5, 3);

console.log(total);
Console output
8

Here, addNumbers(5, 3) returns 8, which is stored in total. The result can then be displayed, compared, or used in another calculation.

Important: return also stops the current function call. Instructions after an executed return statement won’t run.
04 / Default Parameters

Provide a Backup Value

What happens when someone calls your greeting function without providing a name? You can use a default parameter to supply a fallback value.

A friendly default greeting
function greet(name = "Friend") {
  return "Hello, " + name + "!";
}

console.log(greet("Alex"));
console.log(greet());
Console output
Hello, Alex! Hello, Friend!

The default is used when the argument is omitted or explicitly passed as undefined. It isn’t automatically used for every empty value, such as an empty string.

Default parameters are helpful for creating flexible functions that still work when some information is missing.

05 / Function Expressions

Store a Function in a Variable

Functions are values in JavaScript, which means you can store them in variables. This approach is called a function expression.

A function expression
const multiply = function(a, b) {
  return a * b;
};

console.log(multiply(4, 5));
Console output
20

The variable multiply refers to a function. Calling multiply(4, 5) runs it just like a function declaration.

One important difference: function declarations can generally be called earlier in the same scope, but a function expression assigned to const cannot be used before that declaration has been initialized.

06 / Arrow Functions

Write Functions With Modern Syntax

JavaScript offers a shorter way to write many functions using the arrow syntax =>.

An arrow function
const square = (number) => {
  return number * number;
};

console.log(square(6));
Console output
36

For a simple expression, you can make it even shorter.

A concise arrow function
const square = number => number * number;

console.log(square(6));

Arrow functions are especially common when working with arrays and event callbacks. They aren’t identical to traditional functions: for example, they don’t create their own this binding. You’ll encounter those differences as you advance.

07 / Functions in Real Websites

Connect Functions to User Actions

Functions become particularly powerful when they’re connected to webpage events. You can create a reusable function that updates a message whenever someone clicks a button.

A reusable greeting function
function createGreeting(name) {
  return "Welcome to mC0de, " + name + "!";
}

const button = document.getElementById("greetBtn");
const message = document.getElementById("message");

button.addEventListener("click", function() {
  message.textContent = createGreeting("Alex");
});

The createGreeting() function creates the message, while the click handler decides when to display it. Keeping those responsibilities separate makes the code easier to reuse and improve.

Try a function yourself

Enter a name and call the greeting function.

Your greeting will appear here.
Good habit: Give functions descriptive names such as calculateTotal, checkScore, or createGreeting. A good name makes the function’s purpose easier to understand.
🚀 mC0de Challenge

Build a Personalized Greeting Generator

Create a small application that greets visitors by name. Instead of writing separate messages for each person, use a reusable function to generate them.

Your Mission

  1. Create a text input where visitors can enter their names.
  2. Create a function called createGreeting() that accepts a name parameter.
  3. Use return to produce a personalized message.
  4. Add a button that calls the function and displays the greeting.
  5. Provide a friendly fallback when the name field is empty.
  6. Experiment with different names and greeting messages.
Extra challenge: Add a dropdown for different greeting styles, such as Friendly, Professional, and Excited. Create another function that selects the appropriate message, and count how many greetings have been generated.
💻 Interactive JavaScript Sandbox

Build Your Greeting Generator!

Customize the function, change its return value, and run your code. Use the live result to enter different names and greeting styles.

Live Result
Lesson 07 Recap

What You’ve Learned

You’re Building Reusable JavaScript!

Functions are one of the most important tools in programming. You now understand how to organize instructions into reusable pieces that can accept information and produce results.

  • Declare and call functions.
  • Pass information using parameters and arguments.
  • Send results back using return.
  • Provide fallback values with default parameters.
  • Create function expressions and arrow functions.
  • Connect reusable functions to webpage events.
  • Build an interactive greeting generator.

Next up: Arrays — learn how to store and work with collections of information!

Your Learning Journey

0 / 17 Completed

Keep going! Every completed lesson brings you closer to mastering JavaScript.

🎉

Nice Work!

You've learned how to create reusable JavaScript functions, work with parameters and arguments, return results, and write modern arrow functions. You've even built your own personalized greeting generator!