0

I have an array :

const values = [1, 2, 3, 4]

And I'm trying to perform some HOC functions on it ( forEach,every..) for example :

values.forEach(() => {
  const bool = typeof value == "number";
  console.log(bool)
})

The code above works correct ( Without error ) even the result is false and the value should be undefined because I thought that I passed value ( Typo error ) like this :

values.forEach(value => {
      const bool = typeof value == "number";
      console.log(bool)
    })

My question why Javascript does not warn me or throw an error from this method?

2 Answers2

0

It's not because of the forEach method. that's how typeof works, it evaluates the data type of the variable, in case it's not defined, it'll return "undefined".

typeof value === "number"

typeof value //"undefined"

"undefined" === "number" //false
Siddharth
  • 1,200
  • 8
  • 13
0

There's nothing wrong with your code.

typeof will always return a string, so normally we do

  typeof A === 'number'

We normally use === as well. But this shouldn't give an error even it's not a match.

windmaomao
  • 7,120
  • 2
  • 32
  • 36