I have an Ajax request, when it gets to on success function I start to do things with data.d.results like reading through and filtering in another REST API.

However sometimes data.d.results.ID for example is undefined because the array returned is empty.

What condition can I use in my if loop to check if data actually contains any data, if it does I can run my code else I'll just skip this function?

I have seen indexof and contains but I was unsure if this would work for me.

1 Answer 1


Use below code in your success function:

if(data.d.results && data.d.results.length) {
    //Data found - write your code here

For Example:

if(data.d.results && data.d.results.length) {
    for(var i = 0; i < data.d.results.length; i++) {
        console.log("Item ID: " + data.d.results[i].ID);
  • Yes thank you! This is great, what does the && operator mean in this content?
    – Dale
    Jul 11, 2020 at 14:00
  • && is a logical AND operator. It is used to check two conditions together if the results are not undefined AND the results has length (it means it has some items in it). Jul 11, 2020 at 14:39
  • Also, Please Upvote and accept it as an answer if it helped you. Jul 11, 2020 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.