"use strict" without stopping the rise in the function area

My problem is False here. I am learning JavaScript, but not new to programming at all. I understand that lifting, but with strict mode, should not cause an error and be caught either when assigning 6 to an undeclared variable, or document.getElement ... is assigned x, this does not lead to an error, so my diagnosis is that lifting still ongoing. I do not like and want to get rid of the use of strict mode. 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; } } 
+5
source share
2 answers

Declaration variables (i.e. var x; ) are valid for the entire area in which they are written, even if you declare after assignment. This means that "hoisting" : var x; rises to the beginning of the region, and the destination x = 6; excellent because x was declared somewhere in this area.

Strict mode does not change any of this. If you omit the var x; declaration var x; at all; without strict mode, the scope of the variable will be an implicitly global scope.

In ES2015 (aka ES6), lifting can be avoided by using the let keyword instead of var . (Another difference is that variables declared with let are local to the surrounding block, not to the whole function.)

+9
source

There are some strange things that javascript allows, since someone is learning a language, you must learn to deal with good coding methods ( simicolons is another good example). In the case of a lift, it is usually good practice to declare your variables at the top of the area in which they would be raised anyway. As already mentioned, strict mode is not a silver bullet and will not provide this for you.

0
source

All Articles