Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript initialize to undefined or null or ""

Java script has many falsy values as I started learning. I have a program that gets values from a service and loads into an array like this:

function loadNames() {
    Global.names = // what should I use here? undefined, null, "", 0, {} or anything else
    var lnames = getLNames(); // this is doing some magic
        if ( lnames.length !== 0 ) {
            Global.names = new Array();
            for ( var i = 0; i < lnames.length; ++i)
                Global.names[i] = lnames[i];
    }
}

I want to know the right way of resetting Global.names. What is most appropriate here? In code I only want to check like if ( Global.names )

PS: I can't just take the return value into Global.names as the returned object is destroyed later. Hence, I need to do a deep copy

Thanks

like image 203
Kiran Avatar asked May 20 '11 12:05

Kiran


People also ask

Should you use null or undefined 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".

What can I use instead of null in JavaScript?

Empty String String data types are often initalized to null as well. However, for the exact same reasons, we prefer to use an empty string to replace null .

How do you handle undefined and null in JavaScript?

The typeof operator for undefined value returns undefined . Hence, you can check the undefined value using typeof operator. Also, null values are checked using the === operator. Note: We cannot use the typeof operator for null as it returns object .

IS null == in JavaScript?

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations.


3 Answers

Taken from JavaScript: the good parts :

"The if statement changes the flow of the program based on the value of the expression. The then block is executed if the expression is truthy. Here are the falsy values:

  • false

  • null

  • undefined

  • the empty string ''

  • the number 0

  • the number NaN "

So basically if you set your var to any of those values, you'll be able to do a if(var){...}

like image 150
koni Avatar answered Oct 13 '22 00:10

koni


I think you should init GLobal.names = []; and than just check if Global.names.length != 0. If you want to reset it just make it an empty array again.

like image 20
Ionut Popa Avatar answered Oct 12 '22 22:10

Ionut Popa


I think you'd be better off to initialize it as an array and test as

if ( Global.names.length )

Also, if you're just storing strings in the array you can simplify the function as

function loadNames() {
  Global.names = getLNames().concat();
}
like image 25
ic3b3rg Avatar answered Oct 12 '22 23:10

ic3b3rg