Learn your way!

What are Functions in JavaScript

Functions in JavaScript

What are functions in JavaScript?

Functions in JavaScript are blocks of code that are separate and reusable in other parts of your program.  They can contain parameters, return statements or just a set of instructions.  It’s a fundamental way to organize and reuse code in JavaScript.

What does a basic function look like?

 function functionName() {

 // Run specific function related code


If you did not know, the word “function” is a reserved keyword and you can check out a list of reserved keywords here.

Functions vs Procecdures

You can see the different between a function and a procedure easily.  I will quote Toon Krijthe, “A function returns a value and a procedure just executes commands.”.  While functions and procedures may look the same, you can see the difference by looking to see if there is a return statement or not.  In a procedure you will have lines of code without returning any value.  But a function will return a value of some sort.

Defining functions

function name (parameter, parameter, parameter) {



Name of the function.

Parameters are part of the method signature, but they are called arguments when calling a function.  You can have up to 255 parameters, but typically you should stay in the range of up to 5.

Statements is the code that makes up the body of the function.

Function Parameters

Earlier you learned that functions can have parameters.  These parameters are located in the function definition when creating the function.

function name(parameter1, parameter2, parametere3) {


Parameters are a way of passing arguments into a function to perform operations.  Arguments are the real values that are being passed when calling the function.

Parameter Default Values

When calling a function if you fail to pass in an argument on any parameter it will set that value to undefined.  This can be avoided by adding a default value to the function’s definition.

function name(parameter1 = defaultValue, parameter2 = defaultValue, paramter3 = defaultValue) {


Function Return Statement

A function is not a function without a return statement.  Typically functions return a value that is of importance.  It can be a Boolean (true/false) or some value that may be needed in some other part of the code.

function name() {
// Execute code.
 Return value;


Variable Scope

Javascript currently has 2 scopes known as global and local.  A variable can be declared outside a function is global.  When a variable is declared inside a function, than it’s local.  For example, when you declare a variable inside a function like this:

function name() {

 var playerName = “John”;

Once the executing of that function finishes the variable isn’t accessible any where else unless the function returns it.

Leave a Reply

Be the First to Comment!

Notify of
CodeWordjs Newsletter
Get up-to-date tutorials and information weekly on Javascript.
Your Information will never be shared with any third party.