By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,829 Members | 1,798 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,829 IT Pros & Developers. It's quick & easy.

Why do we not introduce let before the variable?

P: 1
Here's my code:

Expand|Select|Wrap|Line Numbers
  1. let person = {
  2.   name: 'Tyron',
  3.   age: 40,
  4.   weekendAlarm: 'No alarms needed',
  5.   weekAlarm: 'Alarm set to 7AM'
  6. };
  7.  
  8. console.log(person['name']);
  9. console.log(person['age']);
  10.  
  11. let day = 'Tuesday';
  12. if (day === 'Saturday' || day === 'Sunday' ) {
  13. alarm = 'weekendAlarm';
  14. } else {
  15.      alarm = 'weekAlarm';
  16. }
  17.  
  18. console.log(person[alarm]);
Runs pretty darn good.
But let introduce "let" before the alarm variable in the if/else loops

Expand|Select|Wrap|Line Numbers
  1. let person = {
  2.   name: 'Tyron',
  3.   age: 40,
  4.   weekendAlarm: 'No alarms needed',
  5.   weekAlarm: 'Alarm set to 7AM'
  6. };
  7.  
  8. console.log(person['name']);
  9. console.log(person['age']);
  10.  
  11. let day = 'Tuesday';
  12. if (day === 'Saturday' || day === 'Sunday' ) {
  13. let alarm = 'weekendAlarm';
  14. } else {
  15.     let alarm = 'weekAlarm';
  16. }
  17.  
  18. console.log(person[alarm]);


I run the code
and boom error?
But why tho? Why shouldn't I use 'let' to introduce my variables in the if/else parts?
Jul 28 '18 #1
Share this Question
Share on Google+
3 Replies


gits
Expert Mod 5K+
P: 5,295
let is used to declare block-scoped variables - thus declaring the variable alarm inside the condition block would not make it available to the outer scope.

https://developer.mozilla.org/en-US/...Statements/let

you can fix it by declaring it outside the block like:

Expand|Select|Wrap|Line Numbers
  1. let alarm;
  2.  
  3. if (day === 'Saturday' || day === 'Sunday' ) {
  4.     alarm = 'weekendAlarm';
  5. } else {
  6.     alarm = 'weekAlarm';
  7. }
  8.  
PS: in your 1st example that works 'darn good' you dont have the variable declared at all before using it - which basicly will make JavaScript create a global variable in the window scope (or any other scope the interpreter would find a variable in with that name before - basicly it crawls up to the topmost scope which would be the top window in which the script currently runs) for you unless there is such a variable already in that or another upper scope - which in that case would just overwrite the value of that variable. Using globals should be avoided in most cases unless there are very strict rules about it that all developers in a project follow about that - else you will most likely end up in the so called 'Globals Hell' - where its more then hard to debug and find out where and when a global variable is set/overwritten/deleted or whatever. To avoid such issues you can force the interpreter into strict mode:

https://developer.mozilla.org/de/doc...ce/Strict_mode

So the answer to your question is: We do (and always should) declare variables - but we have to do it right.
Jul 30 '18 #2

P: 7
I am here to learn some new thing but having a problem understanding this.
Dec 10 '18 #3

gits
Expert Mod 5K+
P: 5,295
well - i could explain it to you - but i cant understand it for you.
Jan 4 '19 #4

Post your reply

Sign in to post your reply or Sign up for a free account.