Basics of working with user-defined functions in JavaScript

Now we will learn how to create our own functions, which can then be used like standard JavaScript functions and methods. Let's look at the syntax for creating our own function.

A function is created with the command function. Next, a space is followed by the name of the function, parentheses, and then curly brackets, in which some code is written:

function func() { // some code }

Let's look at some example. Let's make a function called func, which, when invoked (called), will display an exclamation mark with alert:

function func() { console.log('!'); }

Let's call our function now. To do this, write her name and parentheses:

function func() { console.log('!'); } // Call our function: func(); // shows '!'

You can call our function several times - in this case, each function call will issue a new alert:

function func() { console.log('!'); } func(); // shows '!' func(); // shows '!' func(); // shows '!'

Functions can be invoked before they are defined:

func(); // shows '!' function func() { console.log('!'); }

Make a function that prints your name to the console.

Make a function that prints the sum of numbers from 1 to 100 to the console.