What is the difference between a variable definition and a declaration in JavaScript?

Is this a variable definition or declaration? And why?

var x;

... and is memory reserved for x after this statement?

EDIT: in C extern int x; is a declaration, int x = 5; this is the definition. What is the analogue in JS? Wikipedia says that an ad allocates memory, and a definition assigns a value to that allocated memory.

SECOND EDITING: I think @Deryck's explanation sounds great, but there are some conclusions that don't match his explanation:

 > var x; undefined > x undefined // now it looks like x is defined to the value undefined > y ReferenceError: y is not defined 

If the output of a ReferenceError says y is not declared this will make sense. But often I read that JS has two non-zero values: null and undefined . So var x will be a definition with an undefined value.

+11
javascript
source share
7 answers

var x is a declaration , because you do not determine what value it has, but you declare its existence and the need for memory allocation.

var x = 1 is a declaration and definition, but is shared with x declared at the beginning, while its definition runs on the specified line (destination variables are executed inline).

I see that you already understand the concept of hoisting , but for those who do not, Javascript takes each variable and function declaration and prints it at the beginning (from its corresponding area), then flows down, assigning them in order.

Sounds like you already know that. This is a great resource if you want to move forward, in-depth research. Nevertheless, I feel like you were there before.

Javascript Garden

PS - your analogy between variables with dec / def and JS was in place. What you read on Wikipedia was correct.

+13
source share

Declaring a variable is like telling the compiler (javascript) that I want to use this token x later . It indicates a location in memory, but does not yet contain a value. i.e. it is not undefined

 var x; 

Defining it means assigning it a value that you can do like this:

 x = 10; // defining a variable that was declared previously 

or like this:

 var y = 20; // declaring and defining a variable altogether. 

http://msdn.microsoft.com/en-us/library/67defydd(v=vs.94).aspx http://www.w3schools.com/js/js_variables.asp

+9
source share

I will give you a long answer for a better explanation.

When the javascript engine cannot find a specific variable in memory, it will throw an error. More precisely, when the javascript engine ( execution context ) cannot "refer" to a variable in memory, it will throw a ReferenceError . This is not exactly the same as an ad error , at least in JavaScript.

There is a difference between a not defined error and an undefined value.

So do

 var a = undefined; 

and

 var a; 

both will register the same result, i.e. undefined . This is because when you just do var a; The javascript mechanism allocates memory for a variable and automatically sets it to undefined , which is different from reporting that a does not exist at all - in this case it will throw a ReferenceError .

Lifting

 console.log(a); // undefined var a = 'something'; 

will register undefined because the javascript engine knows that a variable declared somewhere in the code means that the javascript engine actually does something before it executes the code - one of the things it does is raise the variables . Simply put, the code above is the same as

 var a; // hoisted (declared and defined the value 'undefined') console.log(a); // undefined a = 'something' // update the defined value to 'something' 

So yes, the declaration and definition occur together in javascript (automatically - if you yourself do not), and the default value is not undefined .

ES6

Just an extra note.

 const a; 

SyntaxError where an initializer (definition) is needed. const is the only time you need to declare and define manually.

+1
source share
 var x, y, z; var x; var h = 4; i = 4; 

all of the above global variables, if placed on top, (outside of any functions)

Suppose javascript runs a function

 function start() { x = 5*5; } 

global variable x is now equal to 25

Where, as if var x; was not placed outside of any functions, this x variable would be local to this function.

0
source share

You declare JavaScript variables with the var keyword: var carname;

After the declaration, the variable is empty (it does not matter).

To assign a value to a variable, use the equal sign var carname="Volvo";

In computer programs, variables are often declared without value. The value may be something that needs to be calculated, or something that will be provided later, for example, user input. A declared variable without a value will be undefined.

The variable carname will be undefined after the following statement is executed: var carname;

var hoisting

In JavaScript, a variable can be declared after use.

  bla = 2 var bla; // ... // is implicitly understood as: var bla; bla = 2; 

For this reason, it is recommended that you always declare a variable at the top of the functions. Otherwise, this can lead to confusing cases.

When declaring a variable without assigning a value to it, there still needs to be some memory available for it, otherwise you cannot make a reference to the variable later in the program. I don't think it a noticeable amount of memory being used and won't make a difference.

0
source share
 var x; 

This is a variable declaration. In Js, unless you assign a value to a variable in a declaration. By default it will be undefined .

 var x; // declaring x console.log(x); // output: undefined 

But if you haven't even declared a variable, try accessing it. This indicates that the variable is not defined.

 console.log(y); // Output: ReferenceError: y is not defined 

If you need access to objects between JS files, it is recommended to expose one object to the global namespace and declare fields and methods for this object.

File 1:

 var myObject; myObject.myField = "Field!"; 

File 2:

 myObject.prototype.myFunction = function () { return this.myField; }; 

I took from a really good discussion: Equivalent of a C extern declaration in JavaScript

https://github.com/ganqqwerty/123-Essential-JavaScript-Interview-Questions

0
source share

Simply put,

undefined means that the value of the variable is undefined.

not defined means that the variable itself is not defined.

var x;//value is not defined. So, x//undefined

//y variable is not declared or defined. So, y//y is not defined

0
source share

All Articles