Why is my variable not defined in the 'return' eventhough it IS defined just before?

As you can see in the following image, I get an error stating that trd_sntnc is not defined, so the return statement does not run.

However, if you look at the red arrows I have drawn, trd_sntnc is clearly defined and top part of the if statement is clearly running (as evidenced by the console.logt bit of code which shows in the console

What can I do to fix this?

Thanks for your time


Your browser information:

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

Challenge: American British Translator

Link to the challenge:

It is just before, yes, but the let is being defined in that if block…so it only exists within that if. As soon as your code exits those {...}, it’s out of scope.


It lives in the block of code you’ve put it in, not outside of it. JS has what’s called lexical scoping

  let example = 1;

// error
let example;
  example = 1

// not an error

The blocks of code can see variables defined outside them.

But the code outside a given block can’t see variables that are only defined in that block.

This means it’s much easier to avoid errors caused by accidentally overwriting variables when you have some piece of self-contained code inside a block in a specific part of your program.


hm… I would advice to initialice the variable before the if-else and only change the value inside.
Also if you are logging values anyway, try loging trd_sntnc within the if-else to see if that at least works.

Also there is some marking below the name, hovering over it with the mouse might give some failure message.

Personally I think the interpreter might just cause some trouble because you declare the variable twice. Like maybe it’s only taking the second let into account, but then because you don’t go into the else, it’s never actually created?
Interpreters can be a bit more tricky and I am no expert on them. But they are reading the entire code before executing anything and thus certain commands actually cause the interpreter to do actions in the middle of the code, before starting at the top. So it’s not actually as if it just goes from top to bottom. Hence only declaring the variable once might be the solution - plus it’s good practice to not have declerations spread out across the code.

1 Like

Hi @DanCouper

Thanks for your quick reply

I fixed it using var instead of let, but the problem is still not fully solved I think

Now I do get the right value inside trd_sntnc but return res.json(trd_sntnc) brings back ‘undefined’

What can I do to fix this?

Right, let exists to fix the main problem with var: that problem is that var lets you do what you’ve just done. So:

let trd_sntnc;

if (language_change === "american-to-british") {
  trd_sntnc = // translation
} else if (....)
  trd_sntnc = // translation

return res.json(trd_sntnc)

Edit: This is still not great, so for example, could replace all of that with:

// Would add some check invalid text/locale fields first, then:
const britishSentence = translator.americanToBritishSentence(req.body.text);
const americanSentence = translator.britishToAmericanSentence(req.body.text);

return req.json(req.body.locale === "american-to-british" ? britishSentence : americanSentence);

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.