You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1-global scope
2-global scope
3-functional scope
4-Lexical scoping
5-Global scope is accessible from anywhere in the code, we use it when we want a variable to be accessible from multiple parts of the program
functional scope is accessible within the specified function and blocks nested within it
block scope is accessible only within that block and nested other blocks
6- Let respect the scoping rules, but var doesn't
7-Strict mode enforces stricter rules in JavaScript, catching errors and promoting safer coding practices, while sloppy mode is more lenient, allowing potentially unsafe behavior.
5/
-Global Scope: - accessible from everywhere. -most outer scope. -can't access the nested scopes.
-other scopes: - accessible within their range. - specific to their range. -cant be accessed by anything from outer scopes.
6/
-let: allows us to redeclare a variable, it notifies us when we try to create a new variable with the same notation. Most importantly obey scope chain rules.
-Var: doesn't follow the scope chain rules.
7/
strict mode: is a more strict version of JavaScript that enforces certain coding conventions
Sloopy mode: more flexibility but more error-prone.
the difference between global and other scopes: is that global scope is accessible from any other scope, and it has access to is own scope but others scope. we use it we want all the function to have access to a variable from any scope
var : have global/function scope. they're hoisted and are initialized with undefined let : block scope. they are hoisted but not initialized , they can only be accessessed after declaration
Sloppy mode is lenient with common mistakes, while strict mode enforces stricter rules
1- Global scope.
2- let x = 2 is a block, and the others are all global.
3- Function scope.
4- Function scope.
5- Global scope is accessible to every scope, the other scope are only accessible to their own scope and their children.
we use the global scope when we need to access that variable globally.
6- Variables declared by let are only accessible to their own scope, but the variables declared by var are accessible globally.
7- In strict mode we need to declare variables before using them or else we will get an error, but in sloppy mode, we can assign values to undeclared variables and they become global automatically.
1. the scope of variable GLOBAL_DATA is Global scope.
2. in the first example: x has two decalartaions let x=1 Global scope, let x=2 block scope, but the inner declaration has no effect on the outer declaration, but the second example the scopes are Global.
3. the X variable is in the function scope X= 1.
4. it's called lexical scoping
5. Global scope are variable accessible everywhere in the code, the other scope (local/function/block) variablesa are only accesible in the scope.
6. let and var are different when it comes to Scoping, let: Block-scoped variable declaration || var: Function-scoped or global variable declarations and Hoisting Variables declared with var are hoisted and initialized with the value undefined while variables declared with let are hoisted, but they are not initialized until the actual declaration is reached in the code.
7.Strict Mode: Enforces stricter rules and better coding practices, catching common errors and improving code quality while Sloppy Mode: The default mode of JavaScript, it is more lenient and forgiving of coding mistakes.
let x = 2;
local scope :
var x = 2;
3- parent scope : let x = 1;
child scope: let x = 100;
4- nested scoping
5- global scope is accessible everywhere in the code. In the other hand, other types of scope are only accessible to their level and child scopes .
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.
var is problematic with hoisting.
7- JavaScript's strict mode is a way to opt in to a restricted variant of JavaScript, thereby implicitly opting-out of "sloppy mode". Strict mode isn't just a subset: it intentionally has different semantics from normal code. Browsers not supporting strict mode will run strict mode code with different behavior from browsers that do, so don't rely on strict mode without feature-testing for support for the relevant aspects of strict mode. Strict mode code and non-strict mode code can coexist, so scripts can opt into strict mode incrementally.
=> result will be x=2; because the closest and its var declaration
=> result will be x=1
lexical scoping
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
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.
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.
the scope of variable GLOBAL_DATA is the global scope
the scope of the first variable is global , the second one is block scope and for var both of them are global scoped
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
the methode is lexical scoping
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
let is like const it's block and function scoped while var is not block neither function scoped
strict mode forbids setting properties on primitive values. and sloppy mode sometimes hides the errors and makes them silent while executing.
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 }.
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.
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.
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.
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.
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.
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.
1-global scope
2-global scope
3-functional scope
4-Lexical scoping
5-Global scope is accessible from anywhere in the code, we use it when we want a variable to be accessible from multiple parts of the program
functional scope is accessible within the specified function and blocks nested within it
block scope is accessible only within that block and nested other blocks
6- Let respect the scoping rules, but var doesn't
7-Strict mode enforces stricter rules in JavaScript, catching errors and promoting safer coding practices, while sloppy mode is more lenient, allowing potentially unsafe behavior.