Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is a function in JavaScript

Understanding JavaScript Functions

To simplify the concept, think of a function as a small machine in your code. You input some values, the machine does something with these values, and then it outputs something different. In JavaScript, we call these machines functions.

Defining a Function in JavaScript

In JavaScript, you could define a function using the function keyword followed by a name you give to the function. Then, you use parentheses () and curly brackets {}. Whatever is inside the curly brackets is the job your machine (or function) does.

Let's take a look at a simple example:

function sayHello() {
  console.log("Hello, World!");
}

In this code, sayHello is the name of our function. The console.log("Hello, World!") inside the curly brackets {} is what the function does when called.

Calling a Function

Once you've defined a function, it doesn't do anything until you call or invoke it. To call a function, you simply type its name followed by parentheses ().

Let's call the sayHello function:

sayHello();

When the JavaScript engine sees this, it goes back to where you defined sayHello(), runs the code inside, and prints out "Hello, World!".

Functions with Inputs

Now, what if our machine needs some inputs to work with? For example, instead of always saying "Hello, World!", we want it to say hello to a specific person. We can do this by adding parameters to our function.

function sayHello(name) {
  console.log("Hello, " + name + "!");
}

Now when we call sayHello, we need to give it a name:

sayHello("Alice");

This will print out "Hello, Alice!".

Functions with Outputs

In addition to taking inputs, functions can also return outputs. Instead of just printing something out, a function can create a new value and give it back to you. You can do this using the return keyword.

Let's create a function that adds two numbers together:

function addNumbers(num1, num2) {
  return num1 + num2;
}

Now when we call addNumbers, we need to give it two numbers, and it will return their sum.

let sum = addNumbers(5, 7);
console.log(sum); // Prints: 12

Why Use Functions?

Functions are fundamental in JavaScript for several reasons.

  • They prevent repetition: If there's a block of code you find yourself using over and over, that's a good sign it should be a function.
  • They make code readable: Instead of having a long script, you can break it down into smaller, more understandable functions.
  • They can be tested individually: If there's a bug in your program, you can test each function independently to find the problem.

Conclusion

In essence, JavaScript functions are like the Swiss Army knife of coding. They're versatile, practical, and can make your coding life significantly easier. Think of them as your own little customizable machines that can perform various tasks. Each function is unique and can be tailor-made to suit a specific need in your code. So, the next time you find yourself writing the same code over and over or struggling to decipher a script, remember - there's probably a function for that!