Using functions
๐ก Once a JS function is defined (declared), you can use it by referencing its name with parentheses ( ) right after.
Note that a function doesnโt have to have parameters – they can simply be left blank in the parentheses.
Code Example
function greetThePlanet() {
return "Hello world!";
}
Output
"Hello world!"
In the above example,there can be zero, one, or multiple arguments in a function, the function “greetThePlanet” simply returns the phrase “Hello World!”, and it doesn’t require any parameters (arguments) in order to do so.
๐ก If a function does have parameters, youโll need to provide values inside the parentheses when using the function.
Code Example
function square(number) {
return number * number;
}
square(16);
Output
256
In the above example, you have to provide a number inside of the parentheses in order to the function works and receives a proper output. Otherwise, your code will not work and youโll get an error message.