Welcome to the 27th Easy JavaScript tutorial, part of EasyProgramming.net. I apologize for the static in this video! I've gotten a new mic since this was recorded.
The next few tutorials will go over custom functions in JavaScript. We've used functions in the past in the Easy JavaScript series, and now it's time for a deeper dive.
A function is just a block of code designed to perform a particular task. Functions are also reusable. When you call a function, the proper term is invoking a function, and it can be invoked multiple times within the same script (or even from external scripts if a reference to the function definition exists).
Functions can be tied to buttons (which we saw in the onclick tutorial) and be used as methods in objects (something we will cover in another tutorial). They are delcared with the JavaScript reserved keyword named function followed by the name of the function. They can also be assigned to variables. And functions can either return values or just do a basic command and end.
Functions are normally found at the end of your script, or even a completely different file by referencing the file.
Parameters can be defined to receive values from another part of your script. The piece of code sending the parameter is called the argument.
function nameOfFunction(parameter1, parameter2...){ //declare local variables //do something with parameter 1 //do something with parameter 2 //do other things //return value (optional) }
nameOfFunction(argument1, argument2...);
To fork the fiddle and follow along: https://jsfiddle.net/easyjs/8wfvp50j/<
Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!