+ 12
How to make a variable global in javascript?
Suppose there is a function like this.. function myfunction(name){ var names = name+"s" } How would you make that names variable global?
16 Antworten
+ 12
Ipang, BroFarOps 👑 , Kevin Star ,₦ØØⱤ ₳Ⱡł, bahha ,Bunny
Thank you so much guys for helping me out.
I'm really fighting with this for a long time.
It really means a lot. Thank you.
+ 10
Another way (not recomended)
2- don't use any keyword when declaring names. Variables without keywords(var let const) are global by default.
That won't be possible in strict mode
+ 9
Declare it outside function?
var names;
function myfunction(nm) {
names = nm + "s";
}
+ 8
Here is a direct answer from tutorialspoint...
though many answered correctly
https://www.tutorialspoint.com/What-is-the-difference-between-global-and-local-Variables-in-JavaScript
+ 5
By just declaring the variable outside of the function so now you can change the variable's value or modify it.
example( it could look like this):
var name=0;
function upgrade(){
name++;//everytime the upgrade() function runs, the name variable's value will increase by 1
setTimeout(upgrade2,2000);
}
function upgrade2(){
name-=2;//everytime upgrade2() runs the variable's value will decrease by 2
}
And now the name variable's value will change everytime upgrade() runs, but if i declared it into the function upgrade2(),then upgrade() won't effect the variable's value(it may give an error saying that name hasn't been defined), because the variable isn't global it's local if you declare it into a function.
Here:
https://code.sololearn.com/W80kWqZbWd88/?ref=app
+ 5
Ipang No🤔
Here name is an empty string. No problem reading it.
I use the latest Android version of Sl.
+ 4
I have a feeling you're trying to modify a variable by reference which is not possible in Javascript for primitive data types.
you modify a global variable inside a function by using the window object.
correct me if this is not what you want to achieve.
var names = "bunny" ;
function myfunc(name) {
names = name + "s" ;
}
myfunc(names) ;
alert (names) // will show the first variable not the modified by function.
to achieve that without a return that you store again in names.
try this :
window.names = "bunny" ;
function myfunc(name) {
window. names = name + "s" ;
}
myfunc(names) ;
alert (names) //will change according to the function.
otherwise use the usual a function that returns a value and store it again in the global variable.
like:
names = myfunc(name) ;
+ 3
To make a variable global: You have to declare it outside your function, that is, you will have to declare it after your script tag. Example
<script>
Let name = "Sololearn";
function myFunc(name){
let sayName = console.log(name);
}
function anotherFunc(name){
let globalVar = console.log(name + "This was extracted from a global variable");
}
</script>
You can see that in the above code, the variable "name" declared is accessible by everyone but in your case you are declaring yours in a function which can only be used in that function that you declare it. So to make it global, you will have to declare that names outside the function. Example
var names;
function myFunction(name){
names = name + "s";
}
+ 3
Basically, declare it in a global scope... Dunno what a global scope is? Then learn that first.
+ 2
Kevin Star
I was about to post a new question in the forum. Asking why SoloLearn app crashes when I try to do `console.log(name)` or `console.log(window.name)`.
Then you came along with the note about keyword as identifier name. Do you experience that ever? (crash on reading 'name' attribute).
+ 2
Please help me with java
How to create bar chart for number of student appearing in java and show number of male and femlae student separatelyp
+ 2
declare your variable without the word var.. outside the function..thats it
+ 2
simply declare the variable you want to make global outside the function like so..
var names;
function myfunction(name){
names = name+"s";
}
+ 2
ivever timothy the need for a global variable here is to modify the variable without reassigning it a value again.
the way you did it is correct but you will have to add a return to the function. and do
names = myfunction(name) ;
otherwhise if
you modify it inside the function the first var names will not be affected
it is what is called in other programming languages passing by value vs passing by reference.
+ 2
my answers here are solely based on the function written in the first post, which does not return any value and wants to modify a global variable inside it.
that's why it is a bit complicated than just using a global variable outside the function.