As I Java programmer I do not understand why you can check if the array is empty using if(array.length)
. Can someone please elaborate?
6 Answers
Actually array.length
is evaluated as true
when it has at least one element. When array is empty array.length
returns 0
which is evaluated as false
.
So the bottom line is you can use if (array.length)
to check if array is NOT empty.
because first if array
is not defined (undefined) then you can't get the length property of an undefined object.
just use if (typeof array !== 'undefined')
will give the result if array exsit.
as you can see in the example code:
zero.length --> 0
!zero.length --> true
!!zero.length --> false
0
is considered as false in if(0)
or !!0
/!0
, since an empty array have length of 0, it is considered as false, in that case you can't use it to determine if this array exsit or it is just empty.
var array = [];
if (typeof array !== 'undefined'){
console.log('array exsit: true');
} else {
console.log('array exsit: false');
}
var zero = [];
console.log('zero.length --> '+zero.length);
console.log('!zero.length --> '+!zero.length);
console.log('!!zero.length --> '+!!zero.length);
if (typeof array2 !== 'undefined'){
console.log('array2 exsit: true');
} else {
console.log('array2 exsit: false');
}
below is the table for js truthy
and falsy
function checkTruthiness(raw, exp) {
row = $('<tr />').appendTo('#output');
if ( exp ) {
row.append('<td class="truthy">' + raw + '</td>');
} else {
row.append('<td class="falsy">' + raw + '</td>');
}
if ( !exp ) {
row.append('<td class="truthy"> !' + raw + '</td>');
} else {
row.append('<td class="falsy"> !' + raw + '</td>');
}
if ( exp == true ) {
row.append('<td class="truthy">' + raw + ' == true </td>');
} else {
row.append('<td class="falsy">' + raw + ' == true </td>');
}
if ( exp == false ) {
row.append('<td class="truthy">' + raw + ' == false </td>');
} else {
row.append('<td class="falsy">' + raw + ' == false </td>');
}
if ( exp === true ) {
row.append('<td class="truthy">' + raw + ' === true </td>');
} else {
row.append('<td class="falsy">' + raw + ' === true </td>');
}
if ( exp === false ) {
row.append('<td class="truthy">' + raw + ' === false </td>');
} else {
row.append('<td class="falsy">' + raw + ' === false </td>');
}
if ( Boolean(exp) ) {
row.append('<td class="truthy">Boolean(' + raw + ')</td>');
} else {
row.append('<td class="falsy">Boolean(' + raw + ')</td>');
}
}
checkTruthiness('0', 0);
checkTruthiness('NaN', NaN);
checkTruthiness('""', "");
checkTruthiness('false', false);
checkTruthiness('null', null);
checkTruthiness('undefined', undefined);
checkTruthiness('{}', {});
checkTruthiness('[]', []);
checkTruthiness('"0"', "0");
checkTruthiness('"false"', "false");
td {
font-family: Verdana, sans-serif;
font-size:13px;
padding:5px 10px;
}
.truthy {
background-color:#cfc;
}
.falsy {
background-color:#fcc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="output"></table>
You can dynamically calculate the number of elements in the array through the lenght property (the same property can also be applied to a string to count its characters, in the case of the Array () the number of indexes is calculated). Since .lenght returns the number of array elements, if it is empty you will have 0 as the return value.
In most of the programming languages, 0
and null
(undefined
in case of JavaScript) are equivalent to false
. And thus that if
condition is correct.
Unless you want totally different behavior for 0
and null
, then you'll have to do check using ==
(===
in case of JS). This statement returns boolean value.
JavaScript considers NaN
& Empty string also as false.
array.length
will return 0 if the array is empty, 0
is a falsy value.
Falsy values
Examples of falsy values in JavaScript (which translate to false and thus bypass the if block):
false
0
(zero)""
(empty string)null
undefined
NaN
So
var arr = [];
arr.length === 0
arr.length == false
!arr.length === true
Any non-zero number is a truthy value, and will enter the if block.
length
property as well. If your question is "why canarray.length
be used as boolean expression" then it has nothing to do with arrays. You may want to clarify what you actually like to know.