javascript initialize to undefined or null or ""
Solution 1
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){...}
Solution 2
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.
Solution 3
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();
}
Solution 4
You don't have to initialise it to anything. You can do:
if (!Global.names) Global.names = [];
// add members to Global.names
This can be one in one statement:
Global.names = Global.names : [];
If you want to reset it, then:
Global.names = [];
or
delete Global.names;
Solution 5
Setting it to null
is good, as you know that the variable exists but hasn't been assigned a value. This way you can easily see the state - if it's undefined
then you've forgotten to declare it, if it's null
it has been declared but never assigned a value, and if it's an Array
then you can test the length from there.
Kiran
Updated on June 14, 2022Comments
-
Kiran almost 2 years
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
-
fearofawhackplanet almost 13 yearsThis is not correct. If the value is
null
then it has been assigned a value (thenull
value). If you don't assign it anything, it will beundefined
. The language never assigns tonull
implicitly. -
chrisfrancis27 almost 13 yearsWhile that's a great example of initialising a variable with a ternary statement, I don't think it quite answers the OP's question, as he is explicitly trying to clear the value of
Global.names
, while your code will not change its value. -
chrisfrancis27 almost 13 yearsActually yeah, you're right - what I really meant was that it has been assigned a 'falsy' value which is unlikely to be confused later with a valid value such as
[]
. -
fearofawhackplanet almost 13 yearsYes I agree with your approach