0

I using typeof to identify the type of an element.

Here is my code:

<script>
alert("before = " + typeof test);
function test(i) {}
alert("after = " + typeof test);
</script>

In output I am seeing as:

before = function
after = function

When the page loads then the function is not yet defined, then the first alert should say undefined. So why it says function in my output?

learner
  • 4,858
  • 9
  • 47
  • 92

1 Answers1

0

The reason is that a function is hoisted. So it's valid to have:

func()
function func (){
alert('alert')
}
jukben
  • 1,038
  • 7
  • 14