2

Taking the following code snippet as an example:

//variable a,b and c will hold Constructors later
var a = null;
var b = null;
var c;

function setup() {
  //assign those Constructors to variable a,b and c
  a = new ControlsAndInput();
  b = new p5.FFT();
  c = new myOwnConstructor();
}

I noticed some people like to declare and assign the null value to a global variable which will be used later.

The question is: what's the differences between assigning a null value and not assigning a null value to a variable before using it? Is there any difference between var a,b and c in above example, such that unlike a and b, c was not assigned a null value at the start, could this cause any potential bug, why I need to assign null to a variable before using it sometimes

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
WWL
  • 155
  • 2
  • 9

3 Answers3

2

When you don't assign null to a variable you "assign" undefined to it.

var a = null;
var b = null;
var c;

console.log(a, b, c) // => null, null, undefined

You can read answers to this question if you want to know the difference between null and undefined.

If you're curious why people want to add null instead of having undefined - there might be reasons. For example, to pass undefined check in the code. Also, sometimes people can assign not null, but initial values, like false, 0, '', etc; it is a similar action.

But in your particular case you can just use var a, b, c; and it will work and doesn't break any rules.

TylerH
  • 20,799
  • 66
  • 75
  • 101
flppv
  • 4,111
  • 5
  • 35
  • 54
1

What you are essentially doing is saving global variables for later reassignment inside a function. There's no real difference between null and undefined (except that null is an object and undefined is undefined).

Your variables currently look like:

var a = null;
var b = null;
var c;

console.log(a);
console.log(b);
console.log(c);

The simpler way to do this would be to just assign them all at once without a value:

var a, b, c;

Then you can reassign all those as you wish later.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • 1
    “null is an object”..? Doesn’t seem correct. Anyway that would be precisely the difference, assuming that some code made a distinction between the two different values before re-assignment. (And not accessed prior the initial value is .. moot.) – user2864740 Mar 16 '19 at 03:07
  • 5
    It is! `typeof null === "object"`. – Jack Bashford Mar 16 '19 at 03:08
  • So you just rephrase my answer? – flppv Mar 16 '19 at 03:14
  • 4
    `null` is actually a primitive, it is a well-known bug that `typeof null` evaluates to `"object"`, but it would break to much code to fix it. The joys of javascript. See https://stackoverflow.com/questions/18808226/why-is-typeof-null-object – juanpa.arrivillaga Mar 16 '19 at 03:14
1

It's an opinion based question. In most cases, you'll assign them with undefined value. And in some cases, you may use null which is to check if the variable is not undefined. But be aware that null is evil.

Now, let me clarify why we need like that?

var a; // undefined
function setup() {
  a = 10;
}
console.log(a); // a is still undefined
setup();
console.log(a); // a is now 10

So, you are just defining the variable a outside the function scope and assigning its value in the required setup. And whenever you'll call the function the global scope variable is being updated.

Hope this makes sense now.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231