javascript - Return statement not working, with document.write -



javascript - Return statement not working, with document.write -

why doesn't document.write(added); work?

function first(){ var = new date(); var first=math.round(now.getmilliseconds()/20); var second=math.round(now.getmilliseconds()/30); var added=first+second; homecoming added; } first(); document.write(added);

javascript has function scoping, means variables declared in function can't accessed outside of function. return added returns value of added, not variable added itself. if want utilize value, need set in variable declared outside function:

function first(){ var = new date(); var first=math.round(now.getmilliseconds()/20); var second=math.round(now.getmilliseconds()/30); var added=first+second; homecoming added; } var firstresult = first(); document.write(firstresult);

more advanced, related: types of javascript variable scope

javascript return

Comments

Popular posts from this blog

ruby on rails - Devise Logout Error in RoR -

model view controller - MVC Rails Planning -

php - Working with pretty urls on apache and fastcgi after hhvm installation -