Functions
Functions are very important when working with JavaScript. We will also use functions a lot in our examples.
The following code demonstrates the basic syntax of a function. It does not have arguments or the return
statement:
function sayHello() { console.log('Hello!'); }
To call this code, we simply use the following call:
sayHello();
We can also pass arguments to a function. Arguments are variables with which a function is supposed to do something. The following code demonstrates how to use arguments with functions:
function output(text) { console.log(text); }
To use this function, we can use the following code:
output('Hello!');
You can use as many arguments as you like, as follows:
output('Hello!', 'Other text');
In this case, only the ...
Get Learning JavaScript Data Structures and Algorithms - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.