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
  • 6,062
  • 14
  • 79
  • 139

1 Answers1

0

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

func()
function func (){
alert('alert')
}
jukben
  • 1,088
  • 7
  • 16