Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: variable is not defined

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?

like image 870
Andrew Liu Avatar asked Jul 11 '13 01:07

Andrew Liu


People also ask

How do I fix resolve ReferenceError is not defined?

Answer: Execute Code after jQuery Library has Loaded The most common reason behind the error "Uncaught ReferenceError: $ is not defined" is executing the jQuery code before the jQuery library file has loaded. Therefore make sure that you're executing the jQuery code only after jQuery library file has finished loading.

What do you do when a variable is not defined?

This error has the following cause and solution: You used an Option Explicit statement to require the explicit declaration of variables, but you used a variable without declaring it. Explicitly declare the variable, or change the spelling of the variable to match that of the intended variable.

Why is my variable not defined in JS?

This JavaScript exception variable is not defined occurs if there is a non-existent variable that is referenced somewhere. Cause of Error: There is a non-existent variable that is referenced somewhere in the script. That variable has to be declared, or make sure the variable is available in the current script or scope.

How do I fix reference error in JavaScript?

Reference errors in Javascript are mainly thrown when an attempt is made to reference a variable that does not exist or is out of scope. Therefore, in the majority of cases, a ReferenceError can be fixed by making sure that the referenced variable is defined correctly and is being called in the correct scope.


2 Answers

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";.

like image 88
McGarnagle Avatar answered Sep 21 '22 15:09

McGarnagle


Variables are available only in the scope you defined them. If you define a variable inside a function, you won't be able to access it outside of it.

Define variable with var outside the function (and of course before it) and then assign 10 to it inside function:

var value; $(function() {   value = "10"; }); console.log(value); // 10 

Note that you shouldn't omit the first line in this code (var value;), because otherwise you are assigning value to undefined variable. This is bad coding practice and will not work in strict mode. Defining a variable (var variable;) and assigning value to a variable (variable = value;) are two different things. You can't assign value to variable that you haven't defined.

It might be irrelevant here, but $(function() {}) is a shortcut for $(document).ready(function() {}), which executes a function as soon as document is loaded. If you want to execute something immediately, you don't need it, otherwise beware that if you run it before DOM has loaded, value will be undefined until it has loaded, so console.log(value); placed right after $(function() {}) will return undefined. In other words, it would execute in following order:

var value; console.log(value); value = "10"; 

See also:

  • What is the scope of variables in JavaScript?
  • I've Heard Global Variables Are Bad, What Alternative Solution Should I Use?
like image 20
Michał Perłakowski Avatar answered Sep 20 '22 15:09

Michał Perłakowski