Course

Declaring and Invoking Functions

Loading...

In this lesson, you'll learn about declaring and invoking functions in JavaScript, which are essential for structuring and executing code efficiently.

Declaring and Invoking Functions

As we learned in the last section, functions are subprograms designed to perform a particular task.

The code of the function is executed when the function is called. This is known as invoking a function. Let's first revisit the process of creating or defining a function.

There are a few different ways to define a function in JavaScript:

Function Declaration

A Function Declaration defines a named function. To create a function declaration, you use the keyword followed by the name of the function.

function name(parameters) {
  // statements
}

Function Expression

A Function Expression defines a named or anonymous function. An anonymous function is a function that has no name. In the example below, we are setting the anonymous function object equal to a variable.

let name = function(parameters) {
  // statements
}

Arrow Function Expression

An Arrow Function Expression is a shorter syntax for writing function expressions.

const name = (parameters) => {
  // statements
}

Arrow functions are the most modern way to create JavaScript functions. For that reason, we're going to explore them in much more detail in the upcoming lessons! 😊

Invoking a Function

Functions execute when they are called. This process is known as . You can invoke a function by referencing the function name, followed by an open and closed parenthesis: .

Let's explore an example.

First, we’ll define a function named . This function will take one parameter, . When executed, the function will log that back to the console:

function sayHi(name) {
  console.log(`Hi, ${name}`);
}

To our function, we call it while passing in the singular argument. Here I am calling this function with the name :

sayHi('Joe'); // Hi, Joe

Notice how we got back the console log, but we also got back . Why is that? That is connected to the return of the function. If a function does not explicitly return a value, it returns by default. We'll explore this concept further in upcoming lessons.

Loading...

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Function Return