...

/

Functions in JavaScript

Functions in JavaScript

Learn about the fundamental building blocks of most JavaScript programs—functions.

Functions are an essential part of any programming language. They’re the fundamental building blocks of most JavaScript programs, which makes understanding them an essential skill to master.

Defining a function

The most common way to define a function in Javascript is to use a function declaration like the one shown below:

function hello() {
alert('Hello, World!');
}
Function declaration using alert

This creates a function called hello. The code block that describes what the function does is inside the curly braces. This code will create an alert box that displays the message “Hello, World!” Defining the function doesn’t actually do anything, though. In order to actually run the code inside the function, we have to “call” it.

Calling functions

Calling a function will run the block of code inside the function’s body. To call the hello() function that we defined above, we can run the following code:

The function can be called over and over again just by typing its name followed by parentheses.

This is one ...