Closed Adam-Books closed 2 years ago
Any reference provided for above definition will be appreciated.
If you can justify your statement with provided reference will be appreciated.
Global variables are declared outside of a function or declared with a window object for accessibility throughout the program (unless shadowed by locals). If you declare a variable without using var, even if it’s inside a function, it will still be seen as global:
var x = 5; // global
function someThing(y) {
var z = x + y;
console.log(z);
}
someThing(4); // 9
console.log(x); // 5
var x = 5; // global
function someThing(y) {
x = 1; // still global!
var z = x + y;
console.log(z);
}
someThing(4) // 5
console.log(x) // 1
var x = 5; // global
function someThing(y) {
var x = 3; // local
var z = x + y;
console.log(z);
}
someThing(4); // 7
console.log(x); // 5
A global variable is also an object of the current scope, such as the browser window:
var dog = “Fluffy”;
console.log(dog); // Fluffy;
var dog = “Fluffy”;
console.log(window.dog); // Fluffy
To declare JavaScript global variables inside the function, you need to use a window object. For example:
window.value=90;
Now it can be declared inside any function and can be accessed from any function. For example:
function m(){
window.value=100; //declaring global variable by window object
}
function n(){
console.log(window.value); //accessing global variable from other function
}
It’s a best practice to minimize global variables. Since the variable can be accessed anywhere in the program, they can cause strange behavior.
var x = 1;
if (x === 1) {
var x = 2;
console.log(x);
// expected output: 2
}
console.log(x);
// expected output: 2
var x = 5; // global
function someThing(y) {
var x = 3; // local
var z = x + y;
console.log(z);
}
someThing(4); // 7
console.log(x); // 5
var x = 5; // global
function someThing(y) {
x = 1; // still global!
var z = x + y;
console.log(z);
}
someThing(4) // 5
console.log(x) // 1
{
console.log(x + y); // NaN
var x = 1;
var y = 2;
}
{
console.log(x + y); // Uncaught ReferenceError: x is not defined
x = 1;
y = 2;
}
var x = 1 | x = 1 | |
---|---|---|
Strict mode | ✔ | ❌ |
Block scope | ❌ | ✔ |
Function scope | ✔ | ✔ |
Global scope | ✔ | ✔ |
Hoisting | ✔ | ❌ |
Reassigning | ✔ | ✔ |
Please create a PR, so that repository can be updated.
OK... happy new year ☺
Happy New Year 🎉 😄
8: What are global variables?
Your answer is: Global variables are those that are available throughout the length of the code without any scope. The var keyword is used to declare a local variable but if you omit it then it will become a global variable
The problem with global variables is the conflict of variable names of local and global scope. It is also difficult to debug and test the code that relies on global variables.
But I think that:
Global variables are those that are available throughout the length of the code without any scope. The var keyword is used to declare declares a function-scoped or globally-scoped variable but if you omit it then it will become a global variable only.
But var in the 'Block scope' is still a global variable:
297: In JavaScript, What is the difference between var x = 1 and x = 1?
Your Answer is:
‘var x = 1’ will create a variable within the current scope. Given this is declared in a function, x will not be available outside it unless explicitly returned.
‘x = 1’ will create a variable within the global scope. Thus, any other code can access and alter its value. It is generally a bad practice to use variables in a global scope.
But... I think that this is a wrong answer, Because in MDN :
The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value.
and
in strict mode: 'x = 1' is not allowed.
So I hope you correct it.
Because JavaScript has 3 types of scope: