Underscore.js _.isEmpty() Function
- It is used to check whether a list, array, string, object etc is empty or not.
- It first finds out the length of the passed argument and then decides.
- If length is zero, then the output is true otherwise false.
It takes only one argument which is the object.
It returns true if the argument passed is empty, i.e., does not have any elements in it. Otherwise it returns false.
- Passing an empty element to the _.isEmpty() function:
The _.isEmpty() function takes the element from the list one by one and starts counting the length of the array. Each time it encounters an element, it increments length by one. Then, when the array finishes, it checks if the array’s length is zero ( it returns true) or greater than zero (then, it returns false). Here, we have an empty array so the output will be true.
- Passing an array with 6 elements to the _.isEmpty() function:
The procedure for the check function will be same as in the above example. Here, we have 6 elements in the array which implies that at the finish of the array, it’s length will be 6. so, the length is not equal to 0 and hence the answer will be false.
- Passing a list of characters to _.isEmpty() function:
The _.isEmpty() function will work the same as in the examples above. It implies that it does not distinguish between whether the array has numbers, characters or is empty. It will work the same on all the array and find out their length. In this example we have an array of length 4. Hence, the output will be false.
- Passing an element zero to the _.isEmpty() function:
Do not get confused with the empty array and an array containing zero as an element. Since the elements is zero, so you must be thinking that the array is empty. But the array contains an element and since the _isEmpty() calculates the length, therefore the length of the below array will be one which is greater than zero. And hence the output will be false.
These commands will not work in Google console or in firefox as for these additional files need to be added which they didn’t have added.
So, add the given links to your HTML file and then run them.
The links are as follows:
An example is shown below:
My Personal Notes arrow_drop_up
Please Login to comment...