-1

I have got a project, where need to make a check to undifined. How can i do it, because, if i try:

  var sorted_arr = arrayOfParasites.sort((b, a) => a[1] - b[1]);
  console.log(sorted_arr)

It takes an exception:

TypeError: Cannot read property 'sort' of undefined

adiga
  • 34,372
  • 9
  • 61
  • 83
Proger228
  • 688
  • 10

1 Answers1

1

the optional chaining operator in JS. By adding the ? before the method, it will return undefined instead of throwing an error if that method doesn't exist for that variable/type

var arrayOfParasites = undefined;
var sorted_arr = arrayOfParasites?.sort((b, a) => a[1] - b[1]);
  console.log(sorted_arr)
Brandon
  • 389
  • 1
  • 8