Skip to content

Instantly share code, notes, and snippets.

@chetanraj
Created May 2, 2020 15:13
Show Gist options
  • Save chetanraj/c0a04293c2acf53b9aeb1395c81ccd53 to your computer and use it in GitHub Desktop.
Save chetanraj/c0a04293c2acf53b9aeb1395c81ccd53 to your computer and use it in GitHub Desktop.
class Operation {
constructor(a, b) {
this.a = a
this.b = b
}
getSum() {
return this.a + this.b
}
}
let o = new Operation(2, 3);
o.getSum(); // 5
o.getDiff(); // Output - Uncaught TypeError: o.getDiff is not a function
// Check if the function exists
typeof o.getDiff != "undefined" && o.getDiff();
// With the use of optional chaining
o.getDiff?.() // Output - undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment