How do i get the value of the global variable inside a scope .
If i have the same name variable present in same scope .
<script>
var number =2;
var fun= function(numbs){
console.log(number);
//here it displays 2
var number =numbs;
console.log(number);
//here it displays 3
console.log(number);
//how do i get value of global variable here
}
fun(3);
</script>
you should be able to call
window.number
A global variable is really just a property of the window object.
Simply :
console.log(window.number);
From the MDN :
Global variables
Global variables are in fact properties of the global object. In web
pages the global object is window, so you can set and access global
variables using the window.variable syntax.
But you shouldn't have to do this. If you need to go around shadowing, you have a design problem and you should probably fix it.
Related
I learnt about the term variable shadowing in Eloquent Javascript (Chapter 3), but I am trying to understand a precise, basic example of the concept.
Is this an example of shadowing?
var currencySymbol = "$";
function showMoney(amount) {
var currencySymbol = "€";
console.log(currencySymbol + amount);
}
showMoney("100");
That is also what is known as variable scope.
A variable only exists within its containing function/method/class, and those will override any variables which belong to a wider scope.
That's why in your example, a euro sign will be shown, and not a dollar. (Because the currencySymbol containing the dollar is at a wider (global) scope than the currencySymbol containing the euro sign).
As for your specific question: Yes, that is a good example of variable shadowing.
In computer programming, variable shadowing occurs when a variable declared within a certain scope (decision block, method, or inner class) has the same name as a variable declared in an outer scope. This outer variable is said to be shadowed...
so I believe your example is good.
you have a globally named variable that shares the same name as inner method. the inner variable will be used only in that function. Other functions without that variable declaration will use the global one.
Yes, your example is an example of shadowing.
The shadowing will persist in other scenarios too due to how closures work in JavaScript. Here's an example:
var x = -1;
function xCounter() {
var x = 0;
return function() {
++x;
return x;
};
}
console.log(x); // -1
counter = xCounter();
console.log(counter()); // 1
console.log(counter()); // 2
console.log(x); // still -1, global was never touched
Note that in this case, even when xCounter returns, the function it returns still has a reference to its own x and invocations of that inner function have no effect on the global, even though the original has long since gone out of scope.
We cannot define a variable more than once. But we can define in different scopes.
let name="tara"
if(true){
let name="ali"
if(true){
console.log(name)
}
}
variable shadowing is when a variable in a local scope uses its value instead of a variable in a parent scope.So the local variables value is shadowing over the parents.
in the above code there are two name variables defined but they are not defined in the same scope. so console.log(name) will check the local scope if it finds the name variable it uses it, if not it checks parent scope once finds it, it uses that one so it does not go the root.
var role = "Engineer";
console.log(role);
function displayRole(){
role = "developer";
console.log(role);
}
displayRole();
console.log(role);
Notice how the last line of code (console.log) prints developer yet it’s not inside the function scope. This is a good example of shadowing where by the role variable in the global scope has been overwritten by the role in the function scope.
To avoid shadowing, the variable in the function scope should be declared using the var keyword so that it becomes accessible to the function only.
Yes, this is a good example of shadowing. A global scope variable is said to be shadowed by a block scope variable when both have the same name. This is happening in your code and the block scope is shadowing the global scope.
To reference a property of a global object, we can use clearInterval instead of global.clearInterval. console.log instead of window.console.log. Can I confirm that this not having to type 'object.property' is a unique quality of the global object?
Yes, that's correct. Global variables are automatically made properties of the global object, which is named global in node.js, window in browsers.
Note that if you have a local variable with the same name as a global variable, you'll need to use the object.property syntax to access the global variable, since using the name without an object prefix will access the local variable. E.g.
function foo () {
let clearInterval = 0;
window.clearInterval(someVariable);
}
What is the scope of function parameter in Javascript
var greetFunc = function(name){
var something;
}
console.log("Hello" +name);
console.log(something);
I understand the scope of something is just inside the function, it will not exist outside that.
But what about name. Why the value is blank for name variable.
Referencing name outside the function doesn't throw an error like you would expect because it is actually a global variable in every page, part of the global window object. Typing name is the same as window.name.
The something variable causes an error because it hasn't been defined yet. However, the name variable doesn't cause any problems because it is blank by default, at least in Chrome. You are correct that variables created in a function don't exist outside it.
See https://developer.mozilla.org/en-US/docs/Web/API/Window/name for details.
The parameter name is similar to declaring a variable name at the top of the function.
So the scope of a parameter is the function it is a part of.
This is part of the code. I did not get the value of Framesize after button click.
$("#Fs").keyup(function(e){
Framesize=this.value;
alert(Framesize);
});
$("input:button").click(function(e) {
alert(Framesize);
});
var Framesize = null; // Put your variable in the global scope
$("#Fs").keyup(function(e){
Framesize=this.value;
alert(Framesize);
});
$("input:button").click(function(e) {
alert(Framesize);
});
This is because you have declared (even though you didn't use a word var) a variable in one scope of anonymous function and have tried to use it in another scope of anonymous function. Basically the simplest solution to your problem (but maybe not the best for the long term) is to declare the variable Framesize in the global scope of your Javascript code:
var Framesize = null;
Now you can use it safely in every scope in your code and then access it again anywhere, because it's a global variable.
I'm just wondering if it's fine to declare variables with JSON format?
For example, do this:
$(function(){
var global = {
varA : 'This is a global variable from global.varA ~!',
varB : 'This is a global variable from global.varB ~!'
};
alert(global.varA);
alert(global.varB);
});
Instead of this:
$(function(){
var globalVarA = 'This is a global variable from globalVarA ~!',
globalVarB = 'This is a global variable from globalVarA ~!';
alert(globalVarA);
});
The reason why I want to do this is that, It would be easier to look for when I work on a really long JS file. And anything starts with global. I know it is a global variable.
Is it a good practice?
Is there anything I need to put into considerations?
First, this is not JSON format, it's just normal javascript object literal.
Second, since it's valid syntax, you could do this, and this is normal practice to put the variables in a namespace (through there's no namespace concept in javascript).
If you want to make global be global, then you could set it to be a property of the global object window:
$(function(){
var global = {
varA : 'This is a global variable from global.varA ~!',
varB : 'This is a global variable from global.varB ~!'
};
window['global'] = global;
});
Your "new variables" are actually properties of an object literal. They are not variables, nor do they have global scope. However, I don't think it's the worst way to define "references to values". You're using an object as an associative array, which is A-OK.