Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I initialize variables in JavaScript with null or not at all?

I have seen different code examples with variables declared and set to undefined and null. Such as:

var a; // undefined - unintentional value, object of type 'undefined' var b = null; // null - deliberate non-value, object of type 'object' 

If the code to follow these declarations assigns a value to a or to b, what is the reason for using one type of declaration over another?

like image 409
KMcA Avatar asked Oct 30 '12 15:10

KMcA


People also ask

Should you always initialize variables?

In general, yes, it is always better choice to initialize variables (local scoped, automatic storage, specifically) upon definition. Doing so avoids the possibility of using unitialized values, which may lead to undefined behaviour.

Is it necessary to initialize a variable with zero everytime?

Considering we say about C++ (now your question placed in the C++ section) compiler does not need variables be initialized to 0. But it do that automaticaly for arithmetic types defined in a global scope f.e. That also does not mean that someone need it.

Do you have to initialize variables in JavaScript?

Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword as follows. Storing a value in a variable is called variable initialization. You can do variable initialization at the time of variable creation or at a later point in time when you need that variable.

Should you use null in JavaScript?

Only use null if you explicitly want to denote the value of a variable as having "no value". As @com2gz states: null is used to define something programmatically empty. undefined is meant to say that the reference is not existing. A null value has a defined reference to "nothing".


2 Answers

The first example doesn't assign anything to the variable, so it implicitly refers to the undefined value (see 10.5 in the spec for the details). It is commonly used when declaring variables for later use. There is no need to explicitly assign anything to them before necessary.

The second example is explicitly assigned null (which is actually of type null, but due to a quirk of the JavaScript specification, claims to have type "object"). It is commonly used to clear a value already stored in an existing variable. It could be seen as more robust to use null when clearing the value since it is possible to overwrite undefined, and in that situation assiging undefined would result in unexpected behaviour.

As an aside, that quirk of null is a good reason to use a more robust form of type checking:

Object.prototype.toString.call(null); // Returns "[object Null]" 
like image 197
James Allardice Avatar answered Sep 28 '22 22:09

James Allardice


I just saw this link which clarified my question. What reason is there to use null instead of undefined in JavaScript?

Javascript for Web Developers states "When defining a variable that is meant to later hold an object, it is advisable to initialize the variable to null as opposed to anything else. That way, you can explicitly check for the value null to determine if the variable has been filled with an object reference at a later time."

like image 30
KMcA Avatar answered Sep 28 '22 22:09

KMcA