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

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -