Hey. there is a code

if (data) { var list = jQuery('<ul />').attr('class', 'list-ul'); $("body").append(list); list.text(data); } 

if works if data arrives, but if I do

 if(!data) { /// } 

that doesn't work. I also tried to compare data with the empty string "" , wrote data == "" and data === "" , nothing changes. How to correctly determine that the response did not receive data on the request.

    3 answers 3

    Perhaps you have spaces in the string and, accordingly, the string is no longer empty. Use String.trim() to remove extra whitespace:

     var data = " "; data = data.trim(); if (!data) { console.log('error'); } else { var list = jQuery('<ul />').attr('class', 'list-ul'); $("test").append(list); list.text(data); } 
     <div class="test"> </div> 

      Did you check 'else' after your 'if'?

      You can if so:

       if (data)
       {
           var list = jQuery (''). attr ('class', 'list-ul');
           $ ("body"). append (list);
           list.text (data);
       } else {
         // Here, if the data does not come
       }
      

        Thank you all for the answers, the problem was solved by itself. Design

         if (data != "") { /// } else { /// } 

        earlier why not working now works well.