Skip to content

Instantly share code, notes, and snippets.

@hiba-machfej
Created July 27, 2023 11:29
Show Gist options
  • Save hiba-machfej/d058c70738892ea310c5d4fa045e14c0 to your computer and use it in GitHub Desktop.
Save hiba-machfej/d058c70738892ea310c5d4fa045e14c0 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?

@hasnahadd
Copy link

hasnahadd commented Aug 7, 2023

  • => global scope to the tag scripts
  • => first block
  • let x = 1;
  • {
  • let x = 2;
  • }
  • console.log(x);
  • => result will be x=1; will take the global scope
  • => second block
  • var x = 1;
  • {
  • var x = 2;
  • }
  • console.log(x);
  • => result will be x=2; because the closest and its var declaration
  • => result will be x=1
    1. lexical scoping
    1. we need the global scope to access variables from anywhere in code, both inside functions and outside them.
  • The global scope makes variables and functions accessible throughout the program. It's used for values needed globally but should be used cautiously to avoid conflicts.
  • =>var initialized with the value undefined in hoisting they are accessible even if there is scoping
  • => let they are not initialized the value undefined in hoisting

@takidilmi
Copy link

1- GLOBAL_DATA is global scope
2- the two x is a global scope and var doesnt respect anything
3-function scope
4- scope is chain
5-Global scope is accessible everywhere in the code, while other scopes are confined to specific areas. We use global scope for shared variables across the program

6- var causes issues and isn't recommended. let it's recommended for better scoping.

7- Strict mode catches mistakes and enforces good coding. Sloppy mode is forgiving but can lead to hidden errors.

@Ben-Tewfik
Copy link

1 - the scope of variable GLOBAL_DATA global scope
2 - the scope of variable x is global scope
3 - the scope of variable x function scope
4 - the method is chain scope
5- Global scope refers to the outermost level where variables/functions are accessible throughout the program; it's used for constants and widely needed functions. Local and block scopes limit visibility to within functions/blocks, enhancing encapsulation and preventing conflicts between variables.
6 - let has block scope and is preferable for defining variables in modern JavaScript, while var has function scope and can lead to unintended variable hoisting and scope-related issues.

@hocine1212
Copy link

  1. the scope of variable GLOBAL_DATA is the global scope
  2. the scope of the first variable is global , the second one is block scope and for var both of them are global scoped
  3. the scope of variable x=1 is a function scope for outerFn and it's global scoped for run and log functions and the scope of variable x=100 is function scope for the function run
  4. the methode is lexical scoping
  5. the difference between global scope and other scope types is that the global function variables can be refrenced from anywhere else from function,,blocks(if statments,coditions) and we use global scope when we want a variable to be accessible everywhere
  6. let is like const it's block and function scoped while var is not block neither function scoped
  7. strict mode forbids setting properties on primitive values. and sloppy mode sometimes hides the errors and makes them silent while executing.

@SaraBegache
Copy link

  1. The scope of the variable GLOBAL_DATA is global. It is declared using the let keyword outside any function, making it accessible throughout the entire script. When you type console.log(GLOBAL_DATA) in the console, it will print the object { value: 1 }.

  2. In both examples, the scope of variable x is different:

    a) In the first example with let x = 1; { let x = 2; } console.log(x);, the variable x inside the block is separate from the one declared outside the block. The output of console.log(x); will be 1, as it refers to the outer variable x with the value 1.

    b) In the second example with var x = 1; { var x = 2; } console.log(x);, the variable x inside the block reassigns the value of the outer x, as var has function scope (not block scope). The output of console.log(x); will be 2.

  3. The scope of the variable x in this example is confined to the function outerFn. When the log function is called inside run, it will access the x from the outerFn scope and print 1.

  4. This method of scoping, where variables are accessible through nested functions regardless of their level of nesting, is called "Lexical Scope" or "Static Scope." In lexical scoping, the scope of a variable is determined by its position in the source code's lexical hierarchy.

  5. The main difference between the global scope and other scope types (such as function scope or block scope) is the accessibility of variables. Variables declared in the global scope are accessible from anywhere in the code, including inside functions and blocks. On the other hand, variables declared in local scopes (function or block scope) are only accessible within the respective function or block and not from outside.

    The global scope is used when you need a variable to have a global presence and be accessible across the entire codebase. However, it is generally considered best practice to limit the use of global variables to avoid potential conflicts and maintain a clear and modular code structure.

  6. The main differences between let and var when defining variables are:

    • Scope: Variables declared with let have block scope, meaning they are limited to the block (or statement) in which they are declared. On the other hand, variables declared with var have function scope, making them accessible within the entire function they are declared in.

    • Hoisting: Variables declared with let are not hoisted to the top of their scope. They are in a "temporal dead zone" until the line where they are declared is reached. Variables declared with var, however, are hoisted to the top of their scope and can be accessed before their declaration.

    • Redeclaration: Variables declared with let cannot be redeclared within the same block or scope, whereas variables declared with var can be redeclared without any issues.

    Due to these differences, let is generally preferred over var in modern JavaScript code.

  7. The difference between strict mode and sloppy mode pertains to how JavaScript code is executed:

    • Sloppy Mode (Non-strict mode): This is the default mode of JavaScript execution. In this mode, the JavaScript engine is more lenient and allows certain behaviors that can lead to errors or unexpected behavior. For example, undeclared variables can be implicitly created and assigned to the global object (window in browsers). This can lead to unintentional overwriting of global variables.

    • Strict Mode: When strict mode is enabled, certain behaviors that are considered error-prone or unsafe are prohibited. It is enabled by adding the "use strict"; pragma at the beginning of a script or a function. In strict mode:

      • Assigning values to undeclared variables results in a ReferenceError.
      • Deleting undeletable properties or variables results in a TypeError.
      • The use of reserved keywords for future JavaScript features is disallowed.

    Using strict mode is considered a best practice as it helps catch errors early, promotes safer coding practices, and allows the JavaScript engine to perform optimizations. It is recommended to use strict mode in all modern JavaScript code.

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