1

That's one of the weirdest thing I have ever encountered in JavaScript maybe some of you know the reason it behaves the way it does.

If you will run this code you will get undefined

var fn = function(div,speed){

}

(function(){console.log(fn)}())

BUT if we add a variable

var fn = function(div,speed){

}
var weird = true;
(function(){console.log(fn)}())

Now it will return the function

I don't understand what on earth is going on?

It can also work like that

var fn = function(div,speed){
}

!function(){console.log(fn)}()
Dominik
  • 6,078
  • 8
  • 37
  • 61
  • 4
    Missing semicolon. ASI strikes again – VLAZ Mar 17 '21 at 21:07
  • 1
    ohh you right now i see the problem the syntax parser will see that like that var fn = function(div,speed){} (function(){console.log(fn)}) – CaptainMagmelaton Cap Mar 17 '21 at 21:12
  • I suggest getting a linter with an autofix option (ESLint has it), enabling the rule that mandates semicolons and making the linter automatically insert any semicolons it needs. Saves you the hassle. A linter is useful anyway to spot errors like this one. It can also autofix other style related problems. – VLAZ Mar 17 '21 at 21:15

0 Answers0