Why use function expressions (w/ const) instead of just declaring function?

Hello all! I’m working through the introduction to Javascript course, and I’m currently in the functions section. This is the first section that I’m starting to feel a little confused about why certain things are being used instead of others. One example of this is the use of variables within functions, and also the use of function expressions. Codecademy uses function expressions for almost all of their follow along exercises, as opposed to . An example of this is in the Rocks/Paper/Scissors exercise:

const getComputerChoice= () => {
** const randomNumber=**
Math.floor(Math.random() 3); **
** switch (randomNumber) {

** case 0:
*
** return ‘rocks’;**
** case 1: **
** return ‘paper’;**
** case 2:**
** return ‘scissors’;**

Why not do this instead?

function getComputerChoice () {
** let randomNumber=**
Math.floor(Math.random() 3); **
** switch (randomNumber) {

** case 0:
*
** return ‘rocks’;**
** case 1: **
** return ‘paper’;**
** case 2:**
** return ‘scissors’;**
}

Are function expressions and the ‘const’ variable (const randomNumber=) used for reasons related to the rest of the project? Please help me clarify, if you get a chance!

Thank you.
Nick

They say it’s important to be familiar with many ways of writing a function. This helps you identify code that’s been written by other programmers more easily.

In regards to the lesson, they teach the function keyword way first then pretty much scrap that for the rest because arrow functions are a much shorter way and allow for implicit returns* (not sure here but I didn’t manage to do it with functions keyword so there goes that).

*Which looks like: const plantNeedsWater = day => day === 'Wednesday' ? true : false;

Teaching shorthand coding will help you read and code “better” (depending on your definition of better, let’s assume shorter equals better), it takes some getting used to, though. At least for me.

As long as you can read many variants of coding styles you can code in any way you prefer. I’ve tried different ways of coding functions in other excercises besides those that explicitely ask me to use one way or another and it works either way. So it’s really up to you. :slight_smile:

thanks codeRightOn! It’s great to know that I’m not missing anything- that the different ways of writing functions are just variants of the same thing. I am going to play around with trying to write the code for fuctions in the different styles.

1 Like

The difference between const and let is that const declares a variable that cannot be changed and let declares a variable that can change.

Knowing this you can use it to A make your code safer by preventing variables to be redefined by accident once declaring them as a const. And B allow for better readability of your code, i.e. if you declare a variable using let you tell someone else who might read the code that this variable is likely to be used at a later stage to store a new value.

In case of function declaring, using const and arrow notation allows for writing the least amount of code with the same result. Less code means less storage needed and quicker reading by the computer.

Hope this helps :wink: .

1 Like

There is a fairly important difference between ES5 function syntax and ES6 arrow function syntax.

Mentioned above is implicit return. Also we see no curly braces on the body of the function. This only applies where there is but a single expression in the body.

const plantNeedsWater = day => day === 'Wednesday';

When there are multiple statements then a delimited code body is required, and the return keyword must be employed.

Arrow functions map this to the window object so can not be used as methods. They also do not have an arguments object so must have explicit formal parameters.

Above we see that a single parameter need not be wrapped in parens, but when there is no parameter, or there are multiple params, the parens are required.

const foo = () => ...

const foo = a => ...

const foo = (a, b) => ...
2 Likes