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
=> 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.
Uh oh!
There was an error while loading. Please reload this page.