Sitemap
Ace D Baugh

Ace D Baugh

I am a Fine Art Photographer and Fine Code Programmer. I shoot Sony Raw and am a Full Stack Web Developer

JavaScript Functions

2 min readAug 3, 2022
Press enter or click to view image in full size
Photo by Markus Winkler on Unsplash

JavaScript functions are fundamental to creating effective programs and websites. A function is a snippet of code that contains reusable elements that perform tasks, like calculations or comparisons. I was taught that MadLibs are a form of function. And it was one of the first projects I ever built. A function, like a MadLib, can take in information sometimes called parameters or arguments, and places the data into the code snippet to be executed and can easily be reused over and over. Parameters are essentially copies of variables and if altered or changed in any way, you are not affecting the actual variable just a copy of it. Functions can return many different things, from data that is related to the passed variables to a variation of the variable.

The main purpose of functions is to prevent programmers from retyping the same code repeatedly and also to make the overall size of files smaller. In the example below, a many number of users could potentially fill out a form and get a different story each time, but the code didn’t have to be written for each form.

Functions can be used as single-use code, but it is most beneficial to declare a function when you plan to use the same code repeatedly. Functions don’t have to be properly defined by calling the function and then the name like in the example below. An arrow function or even a nameless function can be used to clean up code when the purpose is single-use or will never be called.

function madlib (noun1, noun2, adjective, exclamation) {

Get Ace D Baugh’s stories in your inbox

Join Medium for free to get updates from this writer.

console.log(`One day, a ${noun1} received a ${noun2} for its birthday and was so ${adjective} it yelled ${exclamation}! The End.`);

}

madlib(“toad”, “cheese”, “sad”, “Yay”);

References:

Functions — javascript: MDN. JavaScript | MDN. (n.d.). Retrieved July 18, 2022, from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#:~:text=A%20function%20in%20JavaScript%20is,the%20input%20and%20the%20output

JavaScript Functions. JavaScript functions. (n.d.). Retrieved July 18, 2022, from https://www.w3schools.com/js/js_functions.asp

--

--

Ace D Baugh
Ace D Baugh

Written by Ace D Baugh

I am a Fine Art Photographer and Fine Code Programmer. I shoot Sony Raw and am a Full Stack Web Developer