I’m trying my hand at writing a ridiculously easy-to-follow guide on JavaScript essentials. There might be many of these out there, but this one is mine. Tell me what you think and whether I can clarify or simplify at any point.
In JavaScript, a function is a bunch of code that can be run, or called, repeatedly.
Creating a function
Here is an example of a function being created:
[javascript]
var doStuff = function () {
// Code you want to run goes here!
}
[/javascript]
In this example, we are first creating a variable called doStuff
. doStuff
is then being assigned a function as its value – which means, from now on when we refer to doStuff
, we are referring to a bunch of code that can be called.
Calling a function
Whenever you see parentheses following a variable, it means we are calling a function – the code inside it will be run:
[javascript]
doStuff(); // the doStuff function is being called.
[/javascript]
This is in contrast to just referring to a variable, which is valid, but doesn’t do anything:
[javascript]
doStuff; // no-op
[/javascript]
Arguments
Some functions can change what they do, based on how they are called.
When we are writing code for the browser, we automatically have access to a function named alert
, which tells the web browser to pop up a dialog box. When we call alert
, we need to specify what exactly to show inside that dialog box:
[javascript]
// the alert function is being called with the string ‘Hello!’
alert(‘Hello!’);
[/javascript]
In this example, we are putting a string inside the parentheses. Anything placed inside these parentheses is called an argument. In the case of alert
, the argument determines what text appears in the dialog box that shows up:
If we want to create a function that makes use of an argument, it would look like this:
[javascript]
var takeThis = function (foo) {
// Inside these brackets,
// we have access to a variable named foo.
}
// foo’s value will be “Hello!” when the function is called.
takeThis(“Hello!”);
// foo’s value will be 3 when the function is called.
takeThis(3);
[/javascript]
In this example, we are saying that whatever is passed in as an argument when we call takeThis
will be referred to as foo
as the function runs. We can name the argument whatever we want, but it only affects how it will be referred to inside the function’s code.
Returning
Functions can also give back a result after they’ve been called. This is called returning. Returning is important if we want to receive the result of a function and use it someplace else:
[javascript]
var giveMeSomething = function () {
return “Here you go”;
}
var something = giveMeSomething();
// now something’s value will be “Here you go”
[/javascript]
In this example, we are using the command return
to specify what comes out of the function when it is called. Upon calling giveMeSomething
, we are taking its result and assigning it as the value of a new variable named something
.
Multiple arguments
Functions can accept multiple arguments. It is as simple as separating the values being passed in with a comma. In this example, we are creating a function that can add two numbers together and return the result:
[javascript]
var add = function (addend1, addend2) {
// add the two arguments together and then return the result
return addend1 + addend2;
}
// pass 1 and 2 when calling add and assign its result, 3, to sum
var sum = add(1, 2);
[/javascript]
Having called add
with 1
and 2
as the arguments, those values are then assigned to addend1
and addend2
, respectively.
Calling functions inside other functions
There’s nothing stopping us from calling functions within the code of another:
[javascript]
var sayHello = function () {
alert(“Hello!”);
}
[/javascript]
Here, we’re creating our own function and calling it inside another function we’re creating:
[javascript]
var bar = function () {
// do something
}
var foo = function () {
bar();
}
foo(); // call foo, which will call bar
[/javascript]
Here’s a more complex example where we’re passing in arguments and getting things returned to us:
var bar = function (arg) {
return arg – 1
}
var foo = function (arg) {
return bar(arg) – 1;
}
var result = foo(3);
[/javascript]
Seem complicated? Hover over these steps to see how this code will run:
bar
is defined and assignedfoo
is defined and assignedfoo
is called with3
passed as the argumentfoo
begins to run, witharg
having a value of3
bar
is called witharg
passed as the argumentbar
begins to run, with its own version ofarg
having a value of3
arg
is subtracted by1
, and returned- The result of
bar(arg)
, which is2
, is subtracted by1
, and returned - The result of
foo(3)
, which is1
, is assigned toresult
.
Passing functions into other functions
To take it one step further, we can even pass functions themselves as arguments:
var bar = function () {
// do something
}
var foo = function (callMe) {
// we are assuming that the argument is a function
callMe();
}
// we are not calling bar here,
// merely passing it in as an argument while calling foo
foo(bar);
[/javascript]
This last example might take a bit of time to understand. The difference between merely referring to a function versus calling it is an important distinction.
- When we run
foo(bar);
we are callingfoo
, but not callingbar
– remember, we don’t see a pair of parentheses following it – we’re only referring to it and passing it in. - When foo begins to run, it has access to a variable named
callMe
. Having specified the functionbar
as the argument,callMe
now refers to it as well. callMe
, orbar
, is called.
Why would we want to do this? So we can call foo
multiple times and tell it specifically which function to run each time. In the real world, foo
would do more than just call another function – it might do something with the result of that function, or return something itself.
That’s it for an introduction. I’ll see if I can write something about more complex topics like scopes, this
, recursion, closures, asynchronicity, et cetera – but we’ll start here and see how it goes.
This is very clear and helpful! I like that you made the name of the functions and variables things that explain what they are doing. I will probably end up referring back to this when I go back to dabbling in my web stuff next week- and of course for your class next Thursday! (I do believe?)
Thanks for dabbling in this, helps n00bs like myself ^ ^
Thanks! I should probably specify that descriptively naming variables is very important.
Also – the class is on Monday!
Useful and crystal.