-1

My code is expected to show if my random image is null.
But nothing is happens, I tried to change the undefined state to null as well but it did not have any effect.

getImgUrl (image) {
  if (image === null){
    return './noimage.jpeg';
  }
  return 'https://image.tmdb.org/t/p/w500/' + image;
}
Stphane
  • 3,368
  • 5
  • 32
  • 47
  • 1
    Try console.log(image) and see what you get, most probably image is not null, as a shorthand/safer way you can do if(!image) { return ...} because it might be an empty string, undefined, etc. – Arber Sylejmani Mar 01 '18 at 08:38

1 Answers1

0

you comparing with === here - like explained in:
Which equals operator (== vs ===) should be used in JavaScript comparisons?

The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false.

so you may want to compare using typ conversation, using: if (image == null)

getImgUrl (image) {
  if (image == null){
     return './noimage.jpeg';
  }
  return 'https://image.tmdb.org/t/p/w500/' + image;
}
Skandix
  • 1,916
  • 6
  • 27
  • 36