Home > front end >  JavaScript function always return false even if a value is true
JavaScript function always return false even if a value is true

Time:06-29

I have a function returns true or false but what I see is this function always returns false.

  const isLastImageAttachment = index => {
    const isLastImage =
      filteredImages.uuid === attachments[index].uuid;
    console.log(`isLastImage:`, isLastImage); // true
    return isLastImage ? true : false;
  };

  console.log('isLastImageAttachment()', isLastImageAttachment()); // false

isLastImage returns true or false but isLastImageAttachment always return false even if isLastImage is true.

What is wrong with it?

Attempts

I return isLastImage itself.

  const isLastImageAttachment = index => {
    const isLastImage =
      filteredImages.uuid === attachments[index].uuid;
    console.log(`isLastImage:`, isLastImage); // true
    return isLastImage;
  };

But isLastImageAttachment returns false.

Of course just return true, I got return true

  const isLastImageAttachment = index => {
    return true;
  };

CodePudding user response:

Can you in your function just return isLastImmage itself? Like this:

const isLastImageAttachment = index => {
const isLastImage =
  filteredImages.uuid === attachments[index].uuid;
console.log(`isLastImage:`, isLastImage); // true
return isLastImage;

};

CodePudding user response:

You can simplify it to this:

const isLastImageAttachment = index => filteredImages.uuid === atachments[index].uuid

To fix your problem let's try a little debug. First of all try to put console.log in your code to make sure that filteredImages.uuid and atachments[index].uuid exist. Then try to check their types with help of typeof.

Also just a guess but, filteredImages sounds like an array

CodePudding user response:

If you are 100% sure the is "islastimage" return true your code is correct just change what I mentioned down blow

 const isLastImage = filteredImages.uuid === attachments[index].uuid;
  const isLastImageAttachment = index => {
    const isLastImage =
      filteredImages.uuid === attachments[index].uuid;
    console.log(`isLastImage:`, isLastImage); // true

    // instade of this 
    // return isLastImage ? true : false;
    // just do this (I just simplified your return code line)

    return isLastImage
  };

  console.log('isLastImageAttachment ==>', isLastImageAttachment());
  • Related