Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the JavaScript behavior of a null value?

I had a code problem when testing if some vars are empty or not, and decide to test it in a fiddle:

Testing null values

var result = "";
var Teste = new Object();
Teste.ObjectNew = new Object();
Teste.StringNew = new String();
Teste.NumberNew = new Number();
Teste.ArrayNew = new Array();
Teste.ObjectLiteral = {};
Teste.StringLiteral = "";
Teste.NumberLiteral = 0;
Teste.ArrayLiteral = [];
Teste.ObjectNull = Object(null);
Teste.StringNull = String(null);
Teste.NumberNull = Number(null);
Teste.ArrayNull = [null];
for (var i in Teste) {
  if (Teste[i] == null) {
    result += "<p>Type " + i + " is null: " + Teste[i] + "</p>";
  } else {
    result += "<p>Type " + i + " is not null: " + Teste[i] + "</p>";
  }
}

document.getElementById("result").innerHTML = result;
<div id="result"></div>

The result is:

Type ObjectNew is not null: [object Object]

Type StringNew is not null:

Type NumberNew is not null: 0

Type ArrayNew is not null:

Type ObjectLiteral is not null: [object Object]

Type StringLiteral is not null:

Type NumberLiteral is not null: 0

Type ArrayLiteral is not null:

Type ObjectNull is not null: [object Object]

Type StringNull is not null: null

Type NumberNull is not null: 0

Type ArrayNull is not null:

I tested in Safari, same result.

I was coding in php altogether with JS and had problems in adjust my mind. In php, $var = array() returns NULL, but in JavaScript it seams there is never null value at any type. In EcmaScript definition, null is "primitive value that represents the intentional absence of any object value", but it seams impossible in JavaScript at list by my tests, excluding the case of v = null that i think is a Null type of var.

In addition, I believe AS3 follow the ecmascript concept by split type of for from it's values, the var statement "build" a var as an object apart from values.

So how do we correctly refer to a null value, if there is a way to?

EDIT

I did this test when I had this situation: I created a variable that has the relative directory of a graphic library. If this variable is null, it means I don't wish to change it from the default value (I have a table with default values) during initializing phase of my software, so the system just add the proper http base for the directory. If the variable is not null, it will assume the value was just assigned to it. But if it is an empty space, it means the directory is the root, but will be taken as null, generating an error.

Dinamyc:

var dir = new String(); // should be null
// initializing
dir = ""; // the directory will be the root
// finish ini
if(dir==null) … // assume the default value, but this doesn't work, so how can I know?
like image 509
Gustavo Avatar asked Sep 24 '14 01:09

Gustavo


People also ask

Is null the same as 0 in JavaScript?

Comparisons convert null to a number, treating it as 0 . That's why (3) null >= 0 is true and (1) null > 0 is false. On the other hand, the equality check == for undefined and null is defined such that, without any conversions, they equal each other and don't equal anything else.

What is null data type in JavaScript?

In JavaScript null is "nothing". It is supposed to be something that doesn't exist. Unfortunately, in JavaScript, the data type of null is an object. You can consider it a bug in JavaScript that typeof null is an object.

IS null === undefined JavaScript?

It means null is equal to undefined but not identical. When we define a variable to undefined then we are trying to convey that the variable does not exist . When we define a variable to null then we are trying to convey that the variable is empty.

Does null return TRUE JavaScript?

Another curiosity is that when you loosely check for equality using double equals == , null and undefined will return true . But when you strictly check for equality using triple equals === , null and undefined will return false . This is because null and undefined are both falsy in JavaScript.


2 Answers

In EcmaScript definition, null is "primitive value that represents the intentional absence of any object value", but it seams impossible in JavaScript at list by my tests, excluding the case of v = null that i think is a Null type of var.

Well, all the test cases in your test had a value. I'm not sure why you did exclude

Teste.Null = null;

but it would have worked for it. Also, a Teste.Undefined = undefined would be compare as equal to null.

var result = "";
var Teste = {
    Null: null,
    Undefined: undefined,
    ObjectNew: new Object(),
    StringNew: new String(),
    NumberNew: new Number(),
    ArrayNew: new Array(),
    ObjectLiteral: {},
    StringLiteral: "",
    NumberLiteral: 0,
    ArrayLiteral: [],
    ObjectNull: Object(null),
    StringNull: String(null),
    NumberNull: Number(null),
    ArrayNull: [null]
}
for (var i in Teste) {
    result += "<p>Type "+i+" is"+(Teste[i] == null?"":" not")+" null: "+Teste[i]+"</p>";
}

document.getElementById("result").innerHTML = result;
<div id="result"></div>

So how do we correctly refer to a null value, if there is a way to?

Use the value null. Don't wrap it in anything, or you'd get a wrapper around it (e.g. your array [null]).

If you want to test arrays ([]) or strings ("") for their emptyness (which is a different concept than non-existance of the value), you should check for their .length to be 0.

var dir = new String(); // should be null

No. You've created a Wrapper object (which you never should need) around an empty string here (which you don't seem to want). To declare a variable, but not initialise it, just use

var dir;

or

var dir = null;

and then they will have the value undefined or null, which both are == null in your if-condition.

like image 196
Bergi Avatar answered Oct 27 '22 08:10

Bergi


In JavaScript, null is a special value an object (yes, object - the type of null is object) can have that represents it having no value - this is distinct from it being empty. You can think of {} (empty object) as an empty glass, while null would mean that the glass doesn't even exist. It's also distinct from the variable not being defined at all - when a variable is defined, but set to null, it has a place "reserved" to put the glass (or possibly something else) at some point, but right now that space is not occupied.

As for your test, comparing with either '', 0 or false will give you the is null messages (only when using == and not === for comparison of course). If that's what you're trying to achieve, probably the "proper" (easiest to understand) way to check if a variable has zero or empty value (similar to PHP's empty) is if (!variable) ...

like image 22
fstanis Avatar answered Oct 27 '22 08:10

fstanis