0

I always see

  (function (e){
     console.log("hi")
  })

in libraries Like jQuery but when I try and make something like it in NodeJS it doesn't Log "hi" to the console. What does it mean?

I've tried searching for multiple different solutions online to see what it meant but I couldn't find anything. So, I came to see if anybody knew what it means.

Zac Anger
  • 6,983
  • 2
  • 15
  • 42
Bio Shot
  • 25
  • 6

2 Answers2

2

That's an IIFE mising the second I — an Immediately Invoked Function Expression, but it needs to actually be invoked:

(function (e) {
  console.log("hi")
})() // add parens here
Zac Anger
  • 6,983
  • 2
  • 15
  • 42
  • Thanks! I tried something like that but i didn't work. It now works perfectly. One more question, For example I see this a lot. (!function(e){console.log("hi")})() – Bio Shot Nov 24 '22 at 22:47
  • Using an exclamation instead of wrapping in parens is personal preference. It can lead to issues when using minifiers or other build systems unless there's a semicolon before it (same as with paren-wrapped IIFEs), but if you're working on a personal project or learning, there's no functional difference between `!function(){ ... }()` and `(function() { ... }()`. – Zac Anger Nov 24 '22 at 22:51
2

Self call functions are written like:

(function(...) {

})(); // () needed at the end with a semi column

in your case you are missing the ending.

Note: in some cases you need to put a semi column at the beginning of the self invoke function more explanation here

18jad
  • 413
  • 3
  • 8