The complete guide to JavaScript functions
Functions are one of the most useful features of JavaScript. Learn about function inputs, outputs, variable scopes and more in this article!

Functions are one of the most useful features of JavaScript. Learn about function inputs, outputs, variable scopes and more in this article!
Getting started!
First of all, I encourage you to follow along in this article. It will help you learn better, and also help you to remember what you have done. Let’s start by making a new HTML file with a tag in it:
<html>
<head>
<title>JavaScript Functionstitle>
head>
<body>
<h1>JavaScript :)h1>
<script>
// Our script will go here!
script>
body>
html>
Once that’s done, open it up in your web browser and you’re ready to go! (don’t forget to save and reload the page every time you make a change)
What is a function?
A function is simply a bit of JavaScript code which you can run again and again. You can even give it with different inputs and outputs!
The syntax
For the moment, let’s look at the simplest type of function:
function myFirstFunction() {
var x = 5;
alert(x * 2);
}
First, we’re declaring the function. We do this by saying the word function
and then the name of your function. This is similar to how we declare variable (var variableName
is similar to function functionName
). After the name, there is an empty set of brackets / parentheses / whatever you call these things: ()
.
What’s the point of them if they’re empty though? Well, this is where we put inputs for the function. At the moment, our function doesn’t have any inputs so we leave it empty. I’ll get onto inputs a bit further on in this article (get excited
Apa Reaksimu?






