-1

I have the following function:

function generateTimeStamp(currentDate = null) {
  if (!currentDate) {
    let hours = Math.round(1+(23*Math.random()));
    console.log('hours is ' + hours.toString());
  } else {
    let hours = currentDate.getHours();
  }
  if (hours < 10) {hours = "0" + hours;}
  return hours;

As you can see, I define the variable hours in a way or the other depending on the fact that the parameter currentDate is (or not) null, but I define it in any case.

If you see inside the first if block, the console.log() logs me correctly the value of the variable hours.

However, right after (when I try to perform the last instruction before return), I get an exception saying hours is not defined.

  • Why does the variable hours loses its value outside the if block?
  • How can I fix my issue?

Here a JSFiddle to play around.

Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
  • 1
    https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare-a-variable-in-jav – 3142 maple Apr 24 '18 at 13:24
  • 1
    Do you understand variable scope and what `let` is used for? – Carcigenicate Apr 24 '18 at 13:24
  • 2
    `let` is *specifically* block scoped. – deceze Apr 24 '18 at 13:25
  • You should put `let hours` before `if(!currentDate)`, and assign the value to `hours` in you if blocks. Or simply change `let` to `var`. – 3142 maple Apr 24 '18 at 13:25
  • variables (especially `let` variables) try to exist within the scope that they are defined. This lets you use the same variable name in a different scope without fear of overwriting something you may have needed elsewhere. In your code, initiating `hours` before `if(!currentDate)` will let you use and modify the variable inside the `if/else` and return it afterwards. – Doug Apr 24 '18 at 13:25
  • Please look at this [**JavaScript Guide**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide) - Look for Variable Scope, which also mentions `let` specifically. – Nope Apr 24 '18 at 13:28
  • @deceze, all, thanks for your answers. I know about variable scope, but didn't know about this specificity of let. Thanks for the information. – Matteo NNZ Apr 24 '18 at 13:35
  • @3142maple Suggesting to simply change `let` to `var`, while it "fixes" the issue incidentally, it is not good. To suggest declaration overwrites in code it never a good idea. In the context of the question either `let` or `var` should move to the top of the function. – Nope Apr 24 '18 at 13:39

1 Answers1

3

Because you defined the variable with let within the if block so it is only available in that scope. Unlike var the let keyword is block scoped. You have to put it outside the if statement if you want to use let over var like this:

function generateTimeStamp(currentDate = null) {
  let hours;      
  if (!currentDate) {
    hours = Math.round(1+(23*Math.random()));
    console.log('hours is ' + hours.toString());
  } else {
    hours = currentDate.getHours();
  }
  if (hours < 10) {hours = "0" + hours;}
  return hours;
}
ochs.tobi
  • 3,214
  • 7
  • 31
  • 52