Global Scope Declaration Post ES6

I’m reading a challenge and just learned that in pre-ES6, if we did not declare the variable using “var”, it automatically stored in the global memory (pre ES6).
***** Did this change post ES6 or is it still the same? I mean, if we do not use “let” nowadays when declaring a function in the local scope, does it still automatically go to the global scope?

  **Your code so far**

// Declare the myGlobal variable below this line
var myGlobal = 10; 

function fun1() {
// Assign 5 to 
oopsGlobal = 5; 


// Only change code above this line

function fun2() {
var output = "";
if (typeof myGlobal != "undefined") {
  output += "myGlobal: " + myGlobal;
if (typeof oopsGlobal != "undefined") {
  output += " oopsGlobal: " + oopsGlobal;
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36

Challenge: Global Scope and Functions

Link to the challenge:

JavaScript is backwards compatible, so this is still a risk nowadays. To prevent this, you should use strict mode so undeclared variables are an error.


It still works that way, but you can assume that you’ll always be developing in strict mode to avoid doing stuff like that.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.