473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why do we not introduce let before the variable?

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
3 2767
gits
5,390 Expert Mod 4TB
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
I am here to learn some new thing but having a problem understanding this.
Dec 10 '18 #3
gits
5,390 Expert Mod 4TB
well - i could explain it to you - but i cant understand it for you.
Jan 4 '19 #4

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

Similar topics

3
by: fdsl ysnh | last post by:
--- python-list-request@python.orgдµÀ: > Send Python-list mailing list submissions to > python-list@python.org > > To subscribe or unsubscribe via the World Wide Web, > visit >...
4
by: mike | last post by:
regards: excuse me,can someone introduce me some xhtml webs? i want to see the xhtml web http response. i know one xhtml web "http://www.w3c.org" could someone introduce me some xhtml...
6
by: tim | last post by:
In view of the mixed bag of answers to the "positioning: newbie question" thread, and in view of the fact that the float and the positioning seems to cause trouble (again and again people have...
0
by: ? | last post by:
Hi, After heartily studying XmlSerialization it seems that almost any customization can be implemented to map a class to specific XML schema. Unfortunatelly I can not find how to introduce the...
11
by: Maheshkumar.R | last post by:
Hi groups, How i can introduce some milliseconds delay in application. How i can do achieve this... let me clearly say... (1) I'm displaying slices of medical images. For framerate - for...
3
by: cas | last post by:
Hi, I tried the logon webcontrol after defining some users. Great, but is it possible to introduce a lot of users in one time (import or something like that), because i can't imagine i have to...
43
by: Nikesh | last post by:
Hi friends how about introducing urself ? i m Nikesh from India
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.