Skip to content

Instantly share code, notes, and snippets.

@tech-chieftain
Created January 21, 2023 11:03
Show Gist options
  • Save tech-chieftain/6db017bf5091e4a038fec5ba311eecd2 to your computer and use it in GitHub Desktop.
Save tech-chieftain/6db017bf5091e4a038fec5ba311eecd2 to your computer and use it in GitHub Desktop.

Scopes Discussion

1. What is the scope of variable GLOBAL_DATA in the example below:

<script>
  let GLOBAL_DATA = { value : 1};
</script>

Pretend in the console, that we type: console.log(GLOBAL_DATA);

2. What is the scope of variable x in the examples below:

let x = 1;
{ 
  let x = 2;
}
console.log(x);
var x = 1;
{ 
  var x = 2;
}
console.log(x);

3. What is the scope of variable x in the examples below:

function outerFn(){
    let x = 1;
    function log(){
      console.log(x);
    };
    function run(fn){
      let x = 100;
      fn();
    }
    run(log);
};
outerFn();

4. What do we call this method of scoping:

let x0 = 0;
function fn1(){
  let x1 = 1;
  fn2();
  function fn2(){
    let x2 = 2;
    fn3();
    function fn3(){
      let x3 = 3;
      console.log(x0 + " " + x1 + " " + x2 + " " + x3);
    };
  };
};
fn1();

5. What is the difference between global scope and other scope types and when do we use the global scope?

6. What is the difference between let and var defining a variable?

7. What is the difference between strict mode and sloppy mode?

@AhmadHRai
Copy link

  1. The scope of variable GLOBAL_DATA inside the script tag is everywhere below it on that html document (since it can not be hoisted)

  2. The x defined globally with 'Let' will log 1, while the one defined with 'var' will log 2.

  3. The scope of 'x' is the entire function, the function will log 1 because when the log function is defined, x was equal to 1.

  4. We call it lexical scoping, resolves the variable names when a function is created inside another function

  5. Global is defined globally? it is accessible everywhere

  6. Variables declared by let are only available inside the block where they're defined. Variables declared by var are available throughout the function in which they're declared.

  7. In sloppy mode, a function declaration inside a block may be visible outside the block and even callable. In strict mode, a function declaration inside a block is only visible inside the block

@SufyanCS
Copy link

1- Global scope - variable can be accessed and modified from any script or function in the current web page
2- Global scope
3- Function scope
4- Lexical Scope
5- Global scope is the highest level of scope, It means that any variable declared in global scope is accessible from anywhere in the code.
local and block scope, are more limited in their reach and only apply to certain parts of the code. Local scope applies to variables declared within a function or other block of code, while block scope applies to variables declared within a set of curly braces.
We use global scope when you want to make a variable or function available to all other scripts and functions on a web page.
6- let is block scoped, while var is function scoped. This means that variables declared with let are only accessible within the block they were declared in, while variables declared with var can be accessed throughout the entire function, variables declared with let cannot be re-declared within the same scope, while variables declared with var can be re-declared.
7-Strict mode is a way of writing JavaScript code that enforces stricter rules for certain behaviors. It eliminates some JavaScript silent errors by changing them to throw errors,
Sloppy mode is the default behavior of JavaScript and is less restrictive than strict mode. It allows certain unsafe actions such as accessing the global object and using undeclared variables.

@Haneen-Abdulgllil
Copy link

1. What is the scope of variable GLOBAL_DATA in the example below:

  • Global scope .

Pretend in the console, that we type: console.log(GLOBAL_DATA);

let x = 1; { let x = 2; } console.log(x); var x = 1; { var x = 2; } console.log(x);

x = 1 x = 2

What is the scope of variable x in the examples below:

function outerFn(){ let x = 1; function log(){ console.log(x); }; function run(fn){ let x = 100; fn(); } run(log); }; outerFn();

  • Global scope .

What do we call this method of scoping:

let x0 = 0; function fn1(){ let x1 = 1; fn2(); function fn2(){ let x2 = 2; fn3(); function fn3(){ let x3 = 3; console.log(x0 + " " + x1 + " " + x2 + " " + x3); }; }; }; fn1();

  • lexical scoping

What is the difference between global scope and other scope types and when do we use the global scope?

global scope can be access from any block or scope , but block scope can be access from it's scope. We use global scope when we want to use a variables from any scope.

What is the difference between strict mode and sloppy mode?

Strict mode is a way of writing JavaScript code that enforces stricter rules for certain behaviors. It eliminates some JavaScript silent errors by changing them to throw errors,
Sloppy mode is the default behavior of JavaScript and is less restrictive than strict mode. It allows certain unsafe actions such as accessing the global object and using undeclared variables.

// Strict mode
"use strict";
function test() {
  "use strict";
  var a = 5;
  a = 10;
  a = 15;  // This will throw an error because "a" is  read-only
}
// Sloppy mode
function test() {
  var a = 5;
  a = 10;
  a = 15;  // This will not throw an error 
}

What is the difference between let and var defining a variable?

The main difference between let and var is that scope of a variable defined with let is limited to the block in which it is declared while variable declared with var has the global scope. So we can say that var is rather a keyword which defines a variable globally regardless of block scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment