Introducing Functions

In JavaScript, a function is a block of reusable code designed to perform a specific task when invoked or 'called'. Consider a scenario in which we need to compute something or create a pop-up notification every time a user clicks on a button. In such situations, we would enclose the necessary code within a function and invoke it whenever the button is clicked.

Here is an example of defining a function named sayHello:

In this function, named sayHello, we print "Hello, world!" to the console when the function is invoked or called.

You have learned to define or declare a straightforward function in JavaScript. This is the initial building block in creating reusable code blocks.

Invoking Functions

To utilize a function, we have to invoke or 'call' it by appending parentheses () to the function's name. This process resembles joining a teleconference using a unique meeting link. Just as you join the meeting only when you click the link, the code inside the function is executed only when the function is called.

Let's invoke our sayHello function:

Here, sayHello(); is a function call informing JavaScript to execute the code block within the sayHello function.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal