Skip to content

Instantly share code, notes, and snippets.

@MenarSelamet
Forked from hiba-machfej/NEA - scope.md
Created February 20, 2024 13:00
Show Gist options
  • Save MenarSelamet/22dcb6f52ec8c1c6db3e8fca5862f5c3 to your computer and use it in GitHub Desktop.
Save MenarSelamet/22dcb6f52ec8c1c6db3e8fca5862f5c3 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?

@0Rawan
Copy link

0Rawan commented Feb 20, 2024

Room #3, Aland Rebwar, Fawzi Gharib, Astevan Basi, Hanan Vinar and Rawan Mustafa.

  1. Global
let x = 1; //Global
{ 
 let x = 2; // Block scope
}
console.log(x);  // 1
var x = 1; //Global
{ 
 var x = 2; // Global
}
console.log(x); //2
function outerFn(){
   let x = 1; // It's in  outerFn scope
   function log(){
     console.log(x); 
   };
   function run(fn){
     let x = 100;  //It's in run scope
     fn(); 
   }
   run(log); // 100
};
outerFn(); // 1

 let x0 = 0; // Global
function fn1(){
  let x1 = 1; // It's in fn1 scope
  fn2();
  function fn2(){
    let x2 = 2;  // It's in fn2 scope
    fn3();
    function fn3(){
      let x3 = 3; // It's in fn3 scope
      console.log(x0 + " " + x1 + " " + x2 + " " + x3);
    };
  };
};
fn1(); //0 1 2 3

@PayamRasho
Copy link

  1. Global Data
  2. The first console gonna print 1 and the second one is gonna print 2
  3. Function Block scope it will print 1
  4. The x0 is Global and the other is Local
  5. The Global scope we can access whenever we want for the children the other scopes are accessible only locally or inner Scope

Payam
Vinos
Muhammad
Shvan
Eman

@hanaMohammedAbdullah
Copy link

// Room : Afeaa Taleb , Hana Abdulla , Yousra Yarob , Didam Goran , Helin tayeb
// this is the global scope
//1.
let golabal = "Hana";

// Q2
// let x = 1; // parent scope
// {
// // in block scope child scope
// let x = 2;
// }
// // glabal scope
// console.log(x); // 1
var x = 1;
{
var x = 2;
}
console.log(x); // 2 correct output

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

// let is block scoped and var is function scoped
// var is hoisted and let is not
// let is a keyword and var is not

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

// The global scope is the outermost scope and is accessible from any other scope. It is used to define variables that are accessible from any scope. Other scope types are block scope, function scope, and module scope. We use the global scope when we want to define a variable that is accessible from any scope.

@AbdulrahKh
Copy link

  1. it's in the global scope.
  2. the global scope.
  3. x=1 is inner scope because it's inside the outerFn function, x=100 is inner scope because it's inside run function
  4. lexical scoping
  5. the global scope is like the main room, and inside of that room there's a boxes which is the inner scopes, that inner scope connot be accessed by the global scope.

team room 9

@Dilan-Ahmed
Copy link

Ahmed Isam | Papula Ali | Dilan Ahmed | Muhammad Sabah | Ara Kardo.

  1. it is a Global Scope.

  2. The scope for Variable X in both Let and Var deceleration are global scope and the console log will bring the variables outside the {} blocks but there is a little detail here. the first console log will print the first let variable value which is 1 , for the VAR it will print 2 as the var is a global declared variable itself and can be re assigned . Also another thing here is, when you console log it it gives an Error as the same variable name is declared twice once by let and the other by var which causes the Error .

  3. is a functional scope and will bring back 1.

4.It is called lexical scoping, also known as static scoping. In lexical scoping, the scope of a variable is determined by its position within the source code

  1. The global scope can be accessed globally in the code, but the other type of scoping can only be accessed within the range of its scope .

  2. let can not be re declared but can be re assigned. On the other hand Var is a global declared variable and can be re declared and re assigned.

@Mizu4life
Copy link

Group: Zainab Alnajjar | Aya Hasan | Barham Baper | Kanyaw Yousif | Ninos Dinkha

  1. global scope
  2. global scope when using let, error when using var (it have scope problems)
  3. the main function scope (x=1)

@schinak-m
Copy link

  1. global scope
  2. will print out 1 and 2
  3. its a function scope and will print out 1
  4. Its called a lexical scoping
  5. a global scope is when we can access a variable everywhere in our program but in a local scope it can only be accessed within a specific scope and cant be accessed from the outside. we use global scope when we need to access a variable everywhere.

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