When to declare/initialize variables

Tell us what’s happening:
Why does num not need to be declared or initialized?

Your code so far


function timesFive(num){
return num * 5
}

var answer = timesFive(5);


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.47

Challenge: Return a Value from a Function with Return

Link to the challenge:

num is an argument for a function so it isn’t declared as you would a normal variable with var or let or const. When you make a call to the function, such as

timesFive(5)

then num is automatically set to the value passed into the function (5 in this case).

There are some subtleties with function parameters depending on the type of value passed in. You can read more about it on MDN.

2 Likes

You could say num is a reference to somethign the function expects. When we put timesFive(5), num refers to 5. If we were to say let five=5; timesFive(five) , num would rever to the variable five, which was declared as you can see, on the previous statement.

Just to be clear, since the variable five would be holding a primitive value in this case, the parameter would be passed to the function by value and thus num would be set to a copy of the value and it does not refer to the original value in any way. In other words, you change num however you like and it would not affect the value stored in five.

1 Like

my bad, its gettign too late and i start to not express myself correct

Ohhh man. That’s opening the “Pass by reference” vs “pass by value” can of worms. :eyes:

So all values in javascript are immutable, in that when they’ve been placed in memory, that memory location remains fixed until nothing is referencing it, at which it becomes available for “garbage collection” (freeing up memory). If we were to do this:

function timesFive(num){
  return num*5;
}

let five = 5;
console.log( timesFive(five) ) ;

At the point of the math being performed in timesFive, both five and num point to the same memory location. five has been passed as a reference to an immutable primitive, stored in memory. If we had done this:

function timesFive(num){
  num = num * 5;
  // at this point, we have *changed the memory location to which num points!*
  return num;
}

let five = 5;
console.log( timesFive(five) ) ;

in that one, while both five and num pointed to the same place in memory to begin with, we changed the parameter num within the function (which is bad form for exactly this reason), so num points to one memory location while five still points at the original.

So… by reference or by value? Depends on your perspective.

But to answer the original poster’s question, we don’t explicitly declare num as a variable because we have defined it as a parameter to our function. Javascript is aware of parameters, and makes them a special local variable, accessible only within our function.

1 Like

So to avoid this bad form, rather then use num = num * 5; would you just declare another variable and return that? Like answer = num * 5; and return answer.

1 Like

In general, don’t declare a variable if you are only using the value in one place. I wouldn’t declare an output variable. I’d just return the output.

2 Likes

It is bad form to simply obliterate your original reference, so yes. You could either store it into another variable, if you needed, or simply do whatever processing you need in memory and not bother storing a variable reference. In the original, we are returning num*5, rather than declaring a variable simply to return.

There are times when things are getting complicated, and having a narrative variable name might be useful along the way. But in production code, you’ll often see functions and operations chained in together, passing data from one to the next without creating intermediate variables. For example, this would work fine:

function timesFive(num){
  return num*5;
}

console.log( timesFive( timesFive( timesFive( timesFive( 1) ) ) ) );

// the above could also be done:
const first = timesFive(1);
const second = timesFive( first );
const third = timesFive( second );
const fourth = timesFive( third );
console.log(fourth);

Both of those are fine, they both do the exact same thing - but by declaring many variables, simply to pass the value, is silly. Instead, simply pass the value.

2 Likes