JavaScript Beginner Day 6: Functions

Goal of this Day

Today you will learn:

By the end, you will be able to write reusable blocks of code.

Step 1: What is a Function?

A function is a reusable block of code.

Instead of repeating code, you can call a function.

Step 2: Creating a Function


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

Step 3: Calling a Function


console.log(greet("John"));

Output:


Hello John

Step 4: Parameters and Return Values

Practice


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

console.log(greet("John"));

Task

Create a function that:

Example:


function doubleNumber(num) {
  return num * 2;
}

console.log(doubleNumber(5)); // 10