Why does getTraingingDays() throw an error even though days variable is initialised?

In this project, the first scope issue we try fix involves declaring the days variable before the if block contained in getTrainingDays(), and reassigning its value conditionally.

But why does it throw an error before we change it? One condition will be satisfied (and even it they werent, an else block initilising the days variable throws the same error), so the variable should be initialised no matter what, but we still get a “days is undefined” error.

Original code in the exercise:

const getTrainingDays = event => {
  if (event === 'Marathon') {
    let days = 50;
  } else if (event === 'Triathlon') {
    let days = 100;
  } else if (event === 'Pentathlon') {
    let days = 200;

  return days;


It’s because you define variable inside if{}/else if{} blocks. “let” variables are scoped to block denoted by {}. Also think about your code, what if event is equal “zzzzzzz”, what will happen ?

1 Like

Thanks for the response webdata.

Okay of course, that makes plenty of sense.

Once a block has completed, are locally initialized variables deleted from memory or does that memory just become free to over-write? I’m thinking from an information security point of view, would it be possible then to retrieve local scope information with a memory dump, provided they hadn’t yet been overwritten?


Unfortunately I lack knowledge to answer this question confidently. I recommend reading on this topic here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management

1 Like

I don’t know about in javascript, but in python you can retrieve variables located inside of functions by setting them equal to a new one or a previous one…

number = 2

def add()
    new_number = number + 1
    return new_number

number = new_number

The number variable will now be equal to three and you can reuse it in the future.

There are several other ways to do this that I have found work, but that is just an example.
I would image Javascript has something similar. :grinning:

@webdata Thanks I’ll check that link out!

@8-bitgaming Thanks for the reply. I’m thinking more about a blackbox function which we know has locally initialised variables which it does not return explicitly, nor have we any way of determining their value from the output.

The thought experiment I’m imagining might look something like:

function encrypt (data) {
    let seed = someRandomSeedFunction()
    encryptedData = encrypt(data, seed)
    return encryptedData

If we were able to perform a memory dump and recover the value of the locally initialized seed, you could decrypt the data if the encryption algorithm for encrypt() was known. I hope that example isn’t too contrived. In this situation, knowing what happens to local scope variables could guide better coding practice, hence the curiosity.


javascript variables are not memory, they’re references
security-wise it’s all kinds of irrelevant, in what situation is it reasonable that an attacker is allowed to read memory?