Skip to content

Instantly share code, notes, and snippets.

@muslemomar
Last active October 20, 2022 15:48
Show Gist options
  • Save muslemomar/dd16962b86df04c559ca648c66c69e5a to your computer and use it in GitHub Desktop.
Save muslemomar/dd16962b86df04c559ca648c66c69e5a 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; // global
{
    let x = 2; //local
}
console.log(x); //1
var x = 1; // global
{
    var x = 2; // global
}
console.log(x); //2

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(); //1

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();//0 1 2 3

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?

@shadmustafa
Copy link

shad mustafa-fadi moayad-rokaya amjaad- mhamad marshall
1-Global scope
2- let-(block scope), var-(global scope)
3-function scope
4- lexical scope method
5-global scope : can define anywhere in js , functional scope: can define only inside of function , block scope: can define only inside of block
6-let can't be redeclared and it's a block , functional and global scope, but var can be declared and it is global scope
7-JavaScript by default is in sloppy mode so by adding strict mode above the js codes it It removes different JavaScript silent errors by changing them to throw errors. It repairs mistakes that mark it difficult for JavaScript engines to do optimizations. and also It makes it relaxed to write secure JavaScript.

@rozhiar-tech
Copy link

Room 7 Nina Yousif Rozhiar Maria Shahla

1- global scope because it's inside a function or inside a block

2- the first x is block statement scope because it's initialized with keyword let and the second x is global because it's initialized with var

3-In the following code, x is a global variable for all inner functions, and any changes to x after the log shall not override it since it is declared before the let x = 100 line.

4- Hoisting

5-Global variables are useful for values that are relatively constant, or that many functions in the script must access, such as a session id. A local variable, however, has a limited scope: it exists only within the block that it is declared in.

6-let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.

7-in normal JavaScript, mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable. In normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties.------ let x;

let x =27;

@NanorG
Copy link

NanorG commented Oct 20, 2022

Class Members: Nanor, Aya, Darya, Ruba, and Heyam

  1. Global
  2. If it is defined by var, the scope will always be global(not what is defined inside of a function). While if it is defined by let or const, the scope will be global. Let is block scoped while var is a function scope.
  3. It is a local scope since its accessible and changeable.
  4. If it is outside of the function, it is global and it can be changeable in the future.
  5. The difference between let and var in defining a variable is that var is function scoped while let is block scoped.

@AmenCHW
Copy link

AmenCHW commented Oct 20, 2022

1- The scope is global it can be accessed everywhere.

2- the Variable declared with Let is only accessed inside the block that is declared in so it can't be accessed outside that block, the Variable declared with Var can be also accessed outside the block that was declared in, the variables starting with var are not predictable so better not to use them at all.

3- the X in the outerfn is local for the function itself but it's considered global for its nested functions, and The X of the Run function is local for the Run function alone and it can't be accessed outside of it.

4- It's called the scope chain

5- A- The global: which can be accessed everywhere in your script, B- The Function Scope: can only be accessed inside the function box and the blocks that are in it. C- The Block scope: can only be accessed inside the curly brackets of the block.

6- Let: you can't redeclare it twice, hoisting does not occur, is block scoped BUTTTT var: you can declare it more than twice, hoisting occurs, is function scoped.

7- Strict will give us an error if there is any mistype or if we miss adding brackets or semicolons. but sloppy the javascript will assume what you meant and this may lead to weird bugs in the final project

@bluesky1992-web
Copy link

ROOM 5
ali ibrahim, noor ridha, nma shawkat, zoriana Shatkovska

Q1: its A global SCOPE

Q2: the first example is local or function scope but the second one because it is declared with "var" will be a global variable.

Q3: its function scope.

Q4: Lexical

Q5: the global scope is the outermost scope, its variables can be accessed by the inner scopes like block scope and functional scope.

Q6: the var declaration declares the variable globally regardless of the block scope but the let declaration allows to declaration of variables that are limited to the scope or the block statement.

Q7: in the strict mode for example if you intentionally or unintentionally make a mistake in a code it can't be run but for sloppy mode, it can be run

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