What JavaScript behavior is null?

I had a problem with the code when testing if some vars are empty or not, and decided to test it in a script:

Zero Testing

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> 

Result:

 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, the same result.

I encoded php in general with JS and had problems setting up my mind. In php, $ var = array () returns NULL, but in JavaScript these seams never have a null value in any type. In the EcmaScript definition, null is "a primitive value, which is the intentional absence of any object value", but it cannot be impossible in JavaScript in the list of my tests, except in the case v = null, which I consider to be a type Null var.

In addition, I believe that AS3 follows the split-type ecmascript concept because of its values, the var statement "builds" var as an object other than the values.

So, how do we relate to a zero value, if there is a way?

EDIT

I did this test when I had this situation: I created a variable that has a relative graphics library directory. If this variable is NULL, this means that I do not want to change it from the default value (I have a table with default values) during the initialization phase of my software, so the system simply adds the appropriate http base for the directory. If the variable is not null, it is assumed that the value was simply assigned to it. But if it is an empty space, it means that the directory is the root, but will be accepted 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? 
+6
source share
3 answers

In the EcmaScript definition, null is "a primitive value that represents the intentional absence of any object value", but it cannot be impossible in JavaScript in the list of my tests, except in the case v = null, which, in my opinion, is a Null type var.

Well, all the test cases in your test mattered . I'm not sure why you ruled out

 Teste.Null = null; 

but it would work for that. In addition, a Teste.Undefined = undefined will be compared as 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 relate to a zero value, if there is a way?

Use the null value . Do not wrap it in anything, or you will get a wrapper around it (for example, your array [null] ).

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

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

No. You created a Wrapper object (which you will never need ) around an empty line here (which you don't seem to need). To declare a variable but not initialize it, simply use

 var dir; 

or

 var dir = null; 

and then they will be undefined or null , which are both == null in your if condition.

+3
source

In JavaScript, null is the special value of an object (yes, an object is a type of null - an object) can have something that represents it that does not matter - it distinguishes it from empty. You can represent {} (an empty object) as an empty glass, and null means that the glass does not even exist. It also differs from a variable that is not defined at all - when a variable is defined but set to null , it has a place “reserved” to place the glass (or maybe something else) at some point, but right now, space is not occupied.

As for your test, comparing with '' , 0 or false will give you null messages (only when using == , not === for comparison). If this is what you are trying to achieve, perhaps the “right” (easiest to understand) way to check if a variable is null or empty (similar to PHP empty ) if (!variable) ...

+1
source

==== edit ====

ok, my bad, undefined means to unify it, and null means that it doesn't indicate anything, it doesn't matter.

http://www.2ality.com/2013/10/typeof-null.html

==== / edit ====

there is no such thing as null-string or null-type. null has no type, this means that the variable is not initialized, in other words, the variable points to nowhere. any uninitialized variable is null. like pointers in C ++. a pointer can point to any type, but if its uninitialization indicates a quasi-random place in memory.

SomeType * myVar;

its value is not null, if you use this object, you are useless for a long time, and you have no way to determine if its a valid pointer or not.

SomeType * myVar = 0;

this way you can say that this is an uninitialized pointer, just check if not 0 or not.

in higher-level languages, you don’t need to deal with these problems directly, therefore

var something: AnyObjectType;

automatically null, aka uninitialized.

checking if a string or array is empty or not is another question. in php $ var = array (); if ($ var) evaluates to true since its actual object, not null. empty ($ var) will be the check you use to see if the array really has any content. in js it will be if (myArray && myArray.length) to see if it is a living object and see if it has content.

Lines

can be more complex, since a line with only a space in it is a completely valid object with the actual contents, but you consider it empty in most cases of use.

-1
source

Source: https://habr.com/ru/post/975745/


All Articles