Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
115
rated 0 times [  118] [ 3]  / answers: 1 / hits: 128825  / 11 Years ago, thu, july 11, 2013, 12:00:00

I met this issue sometimes but still don't know what causes it.



I have this script in the page:



$(function(){
var value = 10;
});


But the browser says ReferenceError: value is not defined. However if I go to the browser console and input either



10


or



var value = 10;


either of them can return 10. What is the problem with my script?


More From » variables

 Answers
6

It's declared inside a closure, which means it can only be accessed there. If you want a variable accessible globally, you can remove the var:



$(function(){
value = 10;
});
value; // 10


This is equivalent to writing window.value = 10;.


[#77073] Wednesday, July 10, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
myrap

Total Points: 407
Total Questions: 105
Total Answers: 109

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
myrap questions
Tue, Feb 8, 22, 00:00, 2 Years ago
Wed, Jan 15, 20, 00:00, 4 Years ago
Thu, Oct 24, 19, 00:00, 5 Years ago
Thu, Oct 3, 19, 00:00, 5 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;