简体   繁体   中英

How to modify a global variable inside of a function?

x = 10
function lol(){
    x = 5
}
function rofl(){
    alert(x)
}

In the rofl() function, how can i make the alert popup number 5 ? help me please I'm so noob trying to figure this out for 4 hours now :S

You're close. Really, really close.

The only problem is that you're not actually invoking either of the functions. This is all you're missing: 1

lol();
rofl();

Working demo: http://jsfiddle.net/mattball/VsGWe


1 Well, that and some semicolons.

You dont have to do anything other than call the functions. You have set them up properly, but if you dont call them then nothing will happen.

x = 10
lol();
rofl();

function lol(){
    x = 5
}
function rofl(){
    alert(x)
}

​Live DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM