Create functions JavaScript


show more Break large amounts of JavaScript code into smaller and reusable modular pieces by wrapping statements up into functions. Watch this tutorial to learn how to write (aka define or declare) functions, name functions, call functions, return values, pass parameters, declare variables inside functions, and more. show less
please wait ...

Creating functions

As we start to add more code, we want to make sure that it doesn't get messy and hard to read, so as with all languages, we are going to break apart large amounts of JavaScript into smaller reusable modular pieces. All we are doing is taking several statements, wrapping them up, and giving them a name, and that means we are creating functions. In other languages, these might be called modules or subroutines, but in JavaScript it's functions. To make one, we take the code that we want to enclose-- this could be one line, it could be a hundred lines-- we surround it with curly braces to create a code block to say where this function starts and ends.

We need to say what this is, so we actually use the JavaScript word 'function all lowercase, and because we could have dozens or hundreds of these, we also give it a name. This part, like variables, is up to us. The name has to be one word. There can't be any spaces. You can use letters, numbers, Dollar sign, and underscore, but y...

Creating functions
Video duration: 8m 30s 5h 31m Beginner

Viewers:

Break large amounts of JavaScript code into smaller and reusable modular pieces by wrapping statements up into functions. Watch this tutorial to learn how to write (aka define or declare) functions, name functions, call functions, return values, pass parameters, declare variables inside functions, and more.

Subjects:
Developer Web
Software:
JavaScript
Author:
please wait ...