10

My Problem Lies here I'm learning JavaScript But not new to Programming at all. I understand hoisting, but with strict mode shouldn't this produce an error and be caught either when 6 is assigned to undeclared a variable or document.getElement... is assigned x this doesn't produce an error so my diagnosis is that hoisting is still going on..which i don't like and want to get rid of with using strict. Using Chrome Version 42.0.2311.152 m as my browser

function strictMode(){
    'use strict';
    try {
        x = 6;
        document.getElementById('hoisting').innerHTML = x;
        var x;
     }
     catch(err) {
                    document.getElementById('error_report').innerHTML = 
                        "There was an error that occured (Were in Strict Mode)" +
                            " " + err.message;
               }
}
Zach Hutchins
  • 103
  • 1
  • 4

2 Answers2

15

Variable declarations (i.e. var x;) are valid for the entire scope they are written in, even if you declare after you assign. This is what is meant by "hoisting": the var x; is hoisted to the beginning of the scope, and the assignment x = 6; is fine because x has been declared somewhere in that scope.

Strict mode does not change any of this. It would throw an error if you omitted the var x; declaration altogether; without strict mode, the variable's scope would implicitly be the global scope.

In ES2015 (a.k.a. ES6), hoisting is avoided by using the let keyword instead of var. (The other difference is that variables declared with let are local to the surrounding block, not the entire function.)

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • Good note on implicit global scope, traditionally anything declared in a function scope was only available and 'living' for the duration of the function itself. Are you saying that strict mode is the only way to return scoping to said traditional ways? – Zach Hutchins May 31 '15 at 17:32
  • As long as you diligently write your `var` declarations, strict mode makes no difference whatsoever. Variables are still scoped to the block they're declared in, even without strict mode. Strict mode is only there to catch you if you _forget_ to declare a variable. – Thomas Jun 01 '15 at 14:42
  • Also note that "living" is an entirely different concept from "being in scope". For example, if I write `function foo() { var x = {}; return x; }` then `x` will only be in scope within `foo`, but its value may "live" for a much longer time, depending on what the caller does with it. – Thomas Jun 01 '15 at 14:44
1

There are some weird things javascript allows that, as someone learning the language, you must learn to combat with good coding practices (simicolons are another good example). In the case of hoisting, it is generally good practice to declare your variables at the top of the scope where they would be hoisted to anyway. As already mentioned, strict mode is not a silver bullet and will not enforce this for you.

Community
  • 1
  • 1
aw04
  • 10,857
  • 10
  • 56
  • 89
  • Thanks, I'm really more of a formal programmer and prefer old methods. I read that strict would turn off hoisting thus my test.. It just bothered me that hoisting didn't turn off like the Others have said it did. – Zach Hutchins May 31 '15 at 17:24
  • I think the confusion probably comes from the fact that, as @Thomas points out, strict mode does require you to declare the variable – aw04 May 31 '15 at 17:43