1

I have a JS code like it:

const supportedRegions = ['usa', 'brazil']

const isSupportedRegion = region => supportedRegions.includes(region)

isSupportedRegion('brazil') // true

And it works fine. But if I wrote something like, returns an unexpected error:

const supportedRegions = ['usa', 'brazil']

const isSupportedRegion = supportedRegions.includes

isSupportedRegion('brazil') // error: Uncaught TypeError: Cannot convert undefined or null to object at includes (<anonymous>) at <anonymous>:1:1

Why it is saying that can't convert from undefined or null?? The variable isSupportedRegion is a function!

typeof isSupportedRegion // "function"
isSupportedRegion // ƒ includes() { [native code] }

Someone could explain what is happens, please?

macabeus
  • 4,156
  • 5
  • 37
  • 66
  • 1
    `const isSupportedRegion = supportedRegions.includes` means it loses the `this` context of `supportedRegions` and all you're left with is `Array.prototype.includes`, use `.bind` or `() => supportedRegions.includes(region)` – CertainPerformance May 20 '19 at 06:24

0 Answers0