Javascript Notes – Functions


Basic Syntax

Functions are reusable blocks of code. You can create a function and then call it later. Functions take this simple format:

function functionName() {

//code to run

}

and then to call them later:

functionName();

Arguments

Functions can also take arguments. Lets look at a quick function that adds ten to whatever number is input.

function addTen(num) {

console.log(num+10);

}

addTen(5);

-> 15

Return

Return will always stop the function! It will return the desired result if the conditions are met and then stop the function entirely.

Here are some more sample functions:

Storing Values

coming soon…

setInterval()

setInterval(anotherFunction, interval)

The interval is in milliseconds, so 1 second would be 1000.

clearInterval()

clearInterval(theNumberGivenBySetInterval)

Set interval will provide you with a number that you can use to pass into clear interval to stop it.

Anonymous functions

setInterval(function(){

//some code

}, 2000);

If you do not want to pass in another function you can create an anonymous function that just works with this interval and define it there, but you do not need to name it.

+ There are no comments

Add yours