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?

@nabily4e-dev
Copy link

nabily4e-dev commented Jan 21, 2023

a1. The scope of variable GLOBAL_DATA in the example is global scope, as it is defined outside of any function or block.

a2. In the first example, the scope of variable x is the block in which it is defined, so the console.log(x) will output 1. In the second example, the scope of variable x is the entire function or the global scope in which it is defined, so the console.log(x) will output 2.

a3. In the example, the scope of variable x is the outer function. The function run creates a new variable x with a value of 100, but it does not change the value of the x variable defined in the outer function. So the output is 1.

a4. This method of scoping is called lexical scoping. Lexical scoping, also known as static scoping, is a method of determining the accessibility of a variable based on its location in the source code. In this example, each function (fn1(), fn2(), and fn3()) has its own scope, and each function can access variables from its own scope and the scopes of any containing functions.

In this example, fn1() is called first, and it declares a variable x1 with a value of 1. Inside fn1(), the function fn2() is called, and it declares a variable x2 with a value of 2. Inside fn2(), the function fn3() is called, and it declares a variable x3 with a value of 3. fn3() then uses the console.log() statement to print out the values of all four variables (x0, x1, x2, and x3) which are accessible in its scope and the scopes of its containing functions.

This is the way JavaScript uses to determine the accessibility of variables and it's based on the static structure of the code.

a5. Global scope refers to a variable that is accessible throughout the entire program, whereas other types of scope (such as block or function scope) limit the accessibility of a variable to a certain block or function. Global scope is typically used for variables that need to be accessed by multiple parts of the program.

a6. let and var are used to define variables in JavaScript, but they have different scoping rules. Variables defined with var are function scoped, while variables defined with let are block scoped. This means that variables defined with var are accessible within the entire function they are defined in, while variables defined with let are only accessible within the block they are defined in.

a7. Strict mode is a more restricted version of JavaScript that eliminates some of the more error-prone features of the language, such as automatic semicolon insertion and the ability to use undefined variables. Sloppy mode, also called non-strict mode, is the default mode in JavaScript, and allows for more flexibility but with more potential for errors.

@MohdBasurra
Copy link

#answer 1 : is global, meaning it can be accessed and used anywhere within the script.

#answer 2:The scope of variable x in the first example is limited to the block of code in which it is declared. The scope of variable x in the
second example is global, meaning it can be accessed and modified from anywhere within the program.

#answer 3: x is local to the outerFn function.

#answer4 :called Lexical Scope.

#answer5:Global scope is the highest level of scope and refers to variables that are accessible from anywhere in the code.
local and block scope, refer to variables that are only accessible within a certain context or area of code.
Global scope is used when you need to access a variable from multiple parts of your program.

#answer 6 : difference between let and var when defining a variable is that variables declared with let are block-scoped, meaning they are
only accessible within the block they are declared in. Variables declared with var are function-scoped, meaning they can be
accessed anywhere within the function they were declared in.

#answer7 :In sloppy mode, assigning to NaN does nothing; the developer receives no failure feedback. In strict mode, assigning to NaN throws an exception.

@YassinAbdulrahman
Copy link

YassinAbdulrahman commented Jan 21, 2023

1- globally.
2- first x will be taken from global scope
the second x will be taken from block scope , first seen first serve .
3- first one will be 1 because take from global , second one will be 1 because it was passed as CallBack Fun .
4. Scope Chaining
5. 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.
6- let has a block scope and var has a global scope.
7-In sloppy mode, assigning to NaN does nothing; the developer receives no failure feedback. In strict mode, assigning to NaN throws an exception.

@Jehadalmaliki
Copy link

1/Global scope
2/console.log(x);=1 Global scope , console.log(x);=2 Global scope .
3/Block scope
4/Closure scope
5/Global scope — Global scope contains all of the things defined outside of all code blocks. A code block simply consists of grouped statements inside curly braces ({ }). if statement, loops, function are examples of structure that create a code block. A global variable has global scope. A global variable is accessible from anywhere in the code.
Local Scope — Local scope contains things defined inside code blocks. A local variable has local scope. A local variable is only accessible where it’s declared.
6/var is used to create a function-scoped variable, meaning that the variable is accessible within the entire function in which it is declared, including any nested blocks or functions. If a variable is declared with var outside of any function, it will be a global variable.

let is used to create a block-scoped variable, meaning that the variable is only accessible within the curly braces {} where it is declared. This means that if a variable is declared with let inside a block, it will not be accessible outside of that block.

Another difference is that var is hoisted to the top of the function or global scope while let is not. This means that variables declared with var can be used before they are declared in the code, while variables declared with let cannot.

It's important to note that, const is also used for declaring block scoped variables but it is immutable and cannot be reassigned.

In summary, let and const are recommended over var when you want to create a block-scoped variable, or when you want to create a constant variable that can't be reassigned.
7/In strict mode, you cannot use a variable without declaring it first, whereas in sloppy mode, you can. In strict mode, you cannot use reserved keywords as variable names, whereas in sloppy mode, you can.

In general, strict mode is considered to be a best practice for JavaScript development, as it helps to catch errors early and prevent common mistakes.

It's good to use "use strict" statement at the top of the script or function to make it more secure, reliable, and maintainable.

@sulmi24
Copy link

sulmi24 commented Jan 21, 2023

  1. Global - means that the variable can be accessed and modified from any script or function in the current web page or application.

  2. The first "x" variable is declared with the "let" keyword and is scoped to the block in which it is defined. So the x inside the curly braces has a different value(2) then the x outside(1).
    The second "x" variable is declared with the "var" keyword and is scoped to the entire script. So the x inside the curly braces reassigns the value of the x outside and the final x will be 2.

  3. The scope of the variable "x" in the example is function-scoped, meaning it is only accessible within the function outerFn and the inner functions.

  4. This method of scoping is called lexical scoping or static scoping. In lexical scoping, a variable's scope is determined by its position in the code, specifically, where the variable is defined.

  5. The difference between global scope and other scope types is that variables defined in the global scope are accessible from anywhere in the code, while variables defined in other scopes (such as block-scope, function-scope) are only accessible within the specific scope in which they are defined.

  6. Variables declared with the "var" keyword are function-scoped, meaning they are accessible within the entire function in which they are defined, Variables declared with the "let" keyword are block-scoped, meaning they are only accessible within the block in which they are defined.

  7. Strict mode is a way of running JavaScript code that enforces stricter error handling and prevents certain actions that are considered unsafe or potentially problematic. Sloppy mode is the default mode of executing JavaScript code. It is less strict and allows for certain actions that strict mode does not.

@HamadBakeel
Copy link

1- global
2- error x cannot be redeclared since it was already declared with let in line 1
3- the first declaration of variable x has a lexical scope in log method, the second one though since it was redeclared it has a block scope in run.
4- lexical scoping
5- global scope can be accessed from anywhere, other scoping types like block or lexical aren't accessible from parent scopes only from child one, and it is the go to whenever we have a value we want to be accessible everywhere.
6- var is re-declarable, re-assignable and hoistable, while let is no way re-declarable or hoistable but it can be re-assignable.
7- In sloppy mode, assigning to NaN does nothing; the developer receives no failure feedback. In strict mode, assigning to NaN throws an exception.

@RuqaiahSaif
Copy link

  1. global scope
  2. x=1 ,x=2
  3. global scope x=1
  4. global scope: Any variable declared outside of a function is said to have Global Scope.
    In simple terms, a variable that can be accessed anywhere
    Local Scope
    Any variable that you declare inside a function is said to have Local Scope. You can access a local variable can within a function. If you try to access any variable defined inside a function from outside or another function, it throws an error.

@ramizapk
Copy link

  1. Global scope.

  2. console.log(x);=1 Global scope , console.log(x);=2 Global scope .

3/Block scope.

4/Closure scope.

5/Global scope — Global scope contains all of the things defined outside of all code blocks.
A code block simply consists of grouped statements inside curly braces ({ }).
if statement, loops, function are examples of structure that create a code block. A global variable has global scope. A global variable is accessible from anywhere in the code.

Local Scope — Local scope contains things defined inside code blocks. A local variable has local scope. A local variable is only accessible where it’s declared.

6)var is global scope , var can Redeclared and Reassigned.
let is block scope , let can Reassigned and can't Redeclared

7/In strict mode, you cannot use a variable without declaring it first, whereas in sloppy mode, you can. In strict mode, you cannot use reserved keywords as variable names, whereas in sloppy mode, you can.
In general, strict mode is considered to be a best practice for JavaScript development, as it helps to catch errors early and prevent common mistakes.
It's good to use "use strict" statement at the top of the script or function to make it more secure, reliable, and maintainable.

@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