In my javascript i am trying to check an array if empty.If there is no item in <li> then array will be empty and this should throw error but it is not working. Here is my code
var phrases = [];
$('#listDiv #hiddenItemList').each(function () {
var phrase = '';
$(this).find('li').each(function () {
var current = $(this);
phrase += $(this).text() + ";";
});
phrases.push(phrase);
});
if (phrases === undefined || phrases.length == 0 )
{
$.alert("Please select rate type, high rate and low rate", {
title: "Rates Info",
type: "danger"
});
return false;
}
if(!phrases||!phrases.length)#listDiv #hiddenItemListas ID's must be unique so your each function should only execute 1x (kind of necessary) . Alsophrases === undefinedwill always be false as you definephrases = []. Since you always addphrasetophraseseven if empty you will always have a count, even if you don't have anyli's.