DEV Community

Reece Crook
Reece Crook

Posted on

Learning About JavaScript Functions

Introduction

Functions in JavaScript are the most interesting facet I have learned while coding, to date. Coding as a whole is astounding in that it has no limits, only possibilities. Programming can be applied to any aspect of modern living, from medical applications to space travel, or as common as doing laundry. I am thankful to have been born in this era because I get to experience coding at such a vital time in history. Realizing that I have barely scraped the surface of coding leaves me motivated to dig deeper.

About JavaScript Functions

Functions are a crucial part of JavaScript, they are versatile building blocks that are used consistently when communicating with JavaScript. Functions can be written in a few ways: Function declaration, function expression, and arrow functions. Function declaration is the default or traditional type of function and is the most similar to other languages.

function declarationStyle(parameter1, parameter2){
  console.log(parameter1, parameter2);
}
Enter fullscreen mode Exit fullscreen mode

Function expressions can be constructed in multiple ways, one that includes a function name and one without. A named function expression is a function assigned to a variable and works similarly to the standard function declaration.

const varName = function expressionStyle(parameter1, parameter2){
  console.log(parameter1, parameter2);
}
Enter fullscreen mode Exit fullscreen mode

However, a function expression without a function name(an anonymous function) is often useful when you do not need to re-purpose or alter the function elsewhere, and just want to use the data returned by the function. An anonymous function expression could commonly be used as a parameter for another function.

const varName = function(parameter1, parameter2){
  console.log(parameter1, parameter2);
}
Enter fullscreen mode Exit fullscreen mode

Finally, there are arrow functions. These are the most unique of the three, primarily because they can be written in many different ways. One of which is formatted using a single line, unlike the previously mentioned functions.

parameter => console.log(parameter)
Enter fullscreen mode Exit fullscreen mode

Note that parameters only need parentheses if there is more or less than one parameter, if there is only one parameter it may be written without parentheses like in the example above. This applies to all arrow functions.

Also, similar to function expressions they can be written named or anonymous. Named arrow functions may look like this:

const varName = (parameter1, parameter2) => {
  console.log(parameter1, parameter2);
}
Enter fullscreen mode Exit fullscreen mode

While anonymous arrow functions may look like this:

(parameter1, parameter2) => {
  console.log(parameter1, parameter2);
}
Enter fullscreen mode Exit fullscreen mode

Arrow functions are used in many different situations similar to and vastly different from the examples above. I highly suggest reading Mozilla's for a more in-depth explanation of arrow functions(Or any of the function types) I have briefly mentioned above.

Why I Love JavaScript Functions

JavaScript, out of the few languages that I have experienced so far, I've found to be the most interesting due to the broad yet streamlined application created by its functions. Functions are very similar to how I view decision-making. When met with a decision, I often think something like, "If I choose decision1 I will take x actions and if I choose decision2 I will take y actions." Next, I can make a decision based on which one I believe will lead to the optimal outcome. To me, that sounds awfully similar to pseudocode:

function decisionsAreHard(decision1, decision2){
  if(decision1 > decision2){
    return decision1
  }else{
    return decision2
  }
}
Enter fullscreen mode Exit fullscreen mode

Ever since I started learning about JavaScript my thought process has slowly altered in such a way that I try to turn as many of my actions into functions, like the one above, as I can. They may not always make sense but I find doing this oddly satisfying and genuinely helpful for remembering new expressions and syntax.

Conclusion

Software Engineering is constantly growing and improving, as it always has and always will. Coding has come such an astonishingly long way in the short amount of time it has been around, making me so thankful to be a part of its growth. My outlook on programming throughout my learning experience has consistently evolved. However, one thought that never changed was how eager it has made me to pioneer in the software industry. Software engineering has rapidly become one of the many building blocks in the modern world and it still has such an untapped amount of potential that I can not wait to see how far it will go in my lifetime. I often feel as though I'm learning a deciphered alien language to improve our technological capabilities because I am constantly being impressed by all I am learning about software and what it can do.

Sources

I got my imformation from: Mozilla, FlatIron School(Specifically the software engineering course I am attending), and GeeksForGeeks.

Top comments (0)