Functions in JavaScript

I’d like to recap what I’ve been learning about functions in JavaScript as both an informational tutorial as well as a personal review of an important concept I’ve learned.

Functions are some of the basic building blocks of JavaScript. They allow us to wrap up code and make it reusable throughout the rest of the code. By wrapping up and reusing code we are able to follow an important tenet of programming — DRY. DRY stands for Don’t Repeat Yourself and is something all programmers should aspire to abide by.

To begin describing functions we must first understand the distinction between primitive types and reference types in JavaScripts. Primitive types have a fixed size in memory while reference types have no limit of memory size and refer to a spot in memory. Functions are a reference type therefore every function is unique.

The main thing I want to discuss is the two ways of naming a function. The two ways are function expression and function declaration. Function expression assigns a variable to the function. Therefore the variable refers to the the spot in memory where the function was stored. To declare a function this way the syntax looks like this:

var newFunc = function() { //code goes here};

Function declaration actually names the function. It’s syntax looks like this:

function newFunc() { // code goes here};

These two types of functions are very similar but using function declarations seems to be the preferred method of my instructors so I have been using it more and more.

I hope to learn more about functions more in the future, particularly higher-order functions and recursion. Perhaps I will make a post about these in future.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.