Why do we not introduce let before the variable?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CoderWannabe123
    New Member
    • Jul 2018
    • 1

    #1

    Why do we not introduce let before the variable?

    Here's my code:

    Code:
    let person = {
      name: 'Tyron',
      age: 40,
      weekendAlarm: 'No alarms needed',
      weekAlarm: 'Alarm set to 7AM'
    };
    
    console.log(person['name']);
    console.log(person['age']);
    
    let day = 'Tuesday';
    if (day === 'Saturday' || day === 'Sunday' ) {
    alarm = 'weekendAlarm';
    } else {
         alarm = 'weekAlarm';
    }
    
    console.log(person[alarm]);
    Runs pretty darn good.
    But let introduce "let" before the alarm variable in the if/else loops

    Code:
    let person = {
      name: 'Tyron',
      age: 40,
      weekendAlarm: 'No alarms needed',
      weekAlarm: 'Alarm set to 7AM'
    };
    
    console.log(person['name']);
    console.log(person['age']);
    
    let day = 'Tuesday';
    if (day === 'Saturday' || day === 'Sunday' ) {
    let alarm = 'weekendAlarm';
    } else {
        let alarm = 'weekAlarm';
    }
    
    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?
    Last edited by zmbd; Jul 28 '18, 05:55 AM. Reason: [z:Applied [Code/] format Please read FAQ on asking Questions}]
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    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.

    The let declaration declares re-assignable, block-scoped local variables, optionally initializing each to a value.


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

    Code:
    let alarm;
    
    if (day === 'Saturday' || day === 'Sunday' ) {
        alarm = 'weekendAlarm';
    } else {
        alarm = 'weekAlarm';
    }
    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:

    Der Strict Mode von JavaScript ermöglicht es, sich bewusst für eine eingeschränkte Variante von JavaScript und damit gegen den „sloppy mode“ zu entscheiden. Der Strict Mode ist nicht einfach eine Teilmenge von JavaScript: Er hat bewusst eine andere Semantik als gewöhnlicher Code. Code im Strict Mode und Code außerhalb davon können nebeneinander bestehen. So können Skripte schrittweise auf den Strict Mode umgestellt werden.


    So the answer to your question is: We do (and always should) declare variables - but we have to do it right.
    Last edited by gits; Aug 6 '18, 07:21 AM. Reason: make the scope chain a bit more clear - give a clear answer since the thread title can mislead to wrong assumptions

    Comment

    • cameronhunter
      New Member
      • Dec 2018
      • 7

      #3
      I am here to learn some new thing but having a problem understanding this.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        well - i could explain it to you - but i cant understand it for you.

        Comment

        Working...