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?

4
  • 1
    What exactly is the "error"? Nothing in your code would produce an exception or anything like that. Commented Jun 28, 2020 at 14:48
  • Also what does "HOC" mean? Commented Jun 28, 2020 at 14:49
  • @Pointy Higher Order...Component? Commented Jun 28, 2020 at 14:50
  • 1
    typeof someUndefinedVariable will never result in an error. Commented Jun 28, 2020 at 14:53

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.