Named Function JavaScript

This tutorial covers:
  • What a named function in JavaScript is
  • How to define a named function in JavaScript
  • How to get a JavaScript function's name
  • How to call a named function in JavaScript
Let's get started →

What is a named function in JavaScript?

A named function in JavaScript is a function that has been defined with a name using the special syntax for declaring functions.

function highFive() { return "yeah!"; }


You can see that we start out by using the function keyword:

Named Function JavaScript Function Keyword


Then we give the function we are declaring a name.

In this case the name is highFive:

Named Function JavaScript Function Name


The rest of the code just returns the string "yeah!" when the highFive Named JavaScript function is called.

Named Function JavaScript Function Code


By giving a name to a function we will be able to use it later.

This allows us to also use it in more than one place.

This also allows us to use it more than one time.

How to define a named function in JavaScript?

To define a named function in JavaScript we use the keyword function, give the function a name, and then write the function declaration.

Looking at our previous example:

function highFive() { return "yeah!";}

You can see that all the parts are in there:

  1. function keyword
  2. function name: highFive
  3. function declaration code: return "yeah!";

Once we've defined it we can type it out to see what the JavaScript interpreter took in.

highFive;

Note that we just type out the name.

We don't put an open parentheses nor a close parentheses to evaluate the function.

When we run it the value the JavaScript interpreter returns in the function.

Named JavaScript Function JavaScript Interpreter Value


So you can see we were able to define a named function in JavaScript.

What is a JavaScript function's name?


JavaScript functions are objects.

JavaScript Functions are objects

That means that we can use JavaScript object properties like the name property.

highFive.name;


When we evaluate that expression in the JavaScript interpreter we get the function name back as a string.

JavaScript Function name

So you can see that when we use the name property, we get "highFive" as the name of the function.

How to call a named function in JavaScript?

In the code example we've been using we gave the function the following name: [bold/italics] highFive

function highFive() { return "yeah!";}


We then checked the function's name by using the JavaScript object name property to check the function's name.

highFive.name;


So we defined it with a name and then checked that it did in fact have that name.


We also checked to see what the JavaScript interpreter had when we evaluated the name of the function without any parentheses.

highFive;

This gave us

Named JavaScript Function in JavaScript Interpreter


So we know it's a function and we know it has a name and what that name is.

To call the named JavaScript function we type out the name and then put an open and closed parentheses after the name:

highFive();

Since we aren't passing in any JavaScript Function parameters to the function we don't need to put anything in between the parentheses.

When we press return to evaluate the expression, the named function is evaluated.

Named JavaScript Function is run in the JavaScript interpretert

We can then see that the function has been called.

The result is the string "yeah!"

And that is how you call a named JavaScript function.

Copyright © 2021 JavaScriptFunction.com - All rights reserved.