So when we create a variable in javascript without assigning any value for example here
let name;
This "name" variable will be "undefined".
And to work with this variable either you assign default value or assign "null" as default value but it is totally optional.
If you want to check the type of this "undefined" variable. It returns as "undefined".
But in case of "null" typeof returns as "object".
You can consider null as placeholder but with no value.
Be careful when validating here is why
null === undefined // returns false null == undefined // returns true null === null // returns true
for validating we can do something like this
// Here we will get alert message only if value is not null or not undefined if(name){ alert(name); }
Enough about "null" and "undefined", Let's also consider empty. Empty can be anything Empty array or Empty object.
So basically if you are expecting no data return but getting array with empty data kind of case. Here is how you validate a empty array.
let names = ['Ron']; // Here we have array with single value // Here we check if array has values then alert message. if(names.length){ alert('Array has value'); } we can also do this // Here we check if array has at least one value then alert message. if(names.length > 0){ alert('Array has value'); }
There might be case you see empty string as given here.
let name = ''; // Here we are setting empty string as value // for validation we can check if(name != ''){ alert('Name is not empty string'); }