1

Coming from java, learning angular/typescript: The ! negates booleans or boolean expressions. This is, according to my research, the same in javascript/typescript. But what does the following method do?

  isSelected(product: Product): boolean {
    if (!product || !this.currentProduct) {
      return false;
    }
    return product.sku === this.currentProduct.sku;
  }

What is !product? Product is coming in as a method parameter and then it is asked, if product is not product, or wtf should that mean? :D

codepleb
  • 10,086
  • 14
  • 69
  • 111
  • 3
    It's a way to check for `falsy` values, so if the value of `product` is `falsy`, then do stuff. `falsy` values include: `0, '', false` and some more that I can't remember. – tymeJV Mar 07 '17 at 22:05
  • 1
    https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#user-content-non-null-assertion-operator – dmoo Mar 07 '17 at 22:06
  • Wow that's cool. It's basically a "broader" null-check in java while being way more simple. Thanks guys! – codepleb Mar 07 '17 at 22:10

0 Answers0