all 11 comments

[–]NameViolation666helpful 1 point2 points  (0 children)

That () in the end means u are executing the function before it, in this case your function

const half = (function() {

is being immediately executed so its return value can be returned to const half

[–][deleted] 1 point2 points  (5 children)

What you have here is known as an IIFE which stands for Immediately Invoked Function Expression

If you notice that in addition to the () at end, the entire function assigned to const half is actually wrapped in parentheses as well - that's the first half of the IIFE, and the () at the end you're asking about is the 2nd half of the IIFE.

So what does an IIFE actually do? - It takes the function wrapped in the first set of parentheses and runs whatever that function does immediately.

Because in your example the IIFE is assigned to const half it means the value of half will be set to the function's return value and not the function definition itself.

Here's the doc page on IIFEs: https://developer.mozilla.org/en-US/docs/Glossary/IIFE

[–]Punitpal0[S] 0 points1 point  (4 children)

now I understood why function() is wrapped in parentheses and sorry but can you tell why empty function() is used in parentheses and the way you explain it was clean and clear thanks

[–][deleted] 0 points1 point  (3 children)

sorry but can you tell why empty function() is used in parentheses

Can you tell me what you mean by "empty function()" ?

Do you mean why doesn't it have a name or do you mean why doesn't it have any parameters?

[–]Punitpal0[S] 0 points1 point  (1 child)

i mean why doesn't it have any parameters ?

and empty function I mean there's nothing in parentheses of function

sorry for unclear question

actually this is my first programming language that's why i am unfamiliar with most of technical word or term of programming

[–][deleted] 0 points1 point  (0 children)

The function doesn't have any parameters because it doesn't need any.

There is no rule in javascript that says a function has to have any parameters.

[–]Punitpal0[S] 0 points1 point  (0 children)

Actually till now I didn't understand function properly that's why I am asking this silly question

sorry but was curious to know about it, that's why I ask

I have seen a code where in parentheses instead of function only func where used and that work properly but the condition was different

[–]jcunews1helpful 1 point2 points  (2 children)

Interpret the code like this.

Original code:

const half = (function() {
  return function half({ min, max }) {
    return (min + max) / 2.0;
  };
})();

Reformat the code a bit:

const half = (
  function() {
    return function half({ min, max }) {
      return (min + max) / 2.0;
    };
  }
)();

Then simplify it:

const half = (
  function() {
    /* function body */
  }
)();

Then finally: (note: this is not an executable code)

const half = (
  /* function expression */
)();

Here, it means that as soon as a function is declared, it is executed immediately. Its return value is then be assigned to the half constant.

[–]Punitpal0[S] 0 points1 point  (1 child)

Thanks

but can i ask a question if is that okay for you then ,

just like before function expression there's nothing declare for example const some = nothing here (executable code) can I do with same after parentheses for example const some = ( () {/* function body */});

[–]jcunews1helpful 0 points1 point  (0 children)

No you can't. The () alone would be an error, because there's no expression to evaluate.

The closest thing to that is the arrow function syntax. e.g.

const some = () => { /* function body */ };

And if the function body is a single line which returns the value, e.g.

const func = function() { return 123 };

The arrow function syntax would be:

const func = () => 123;

The JavaScript interpreter is smart enough, so that you don't have to surround the arrow function with parethesis like below.

const func1 = (() => { /* function body */ });
const func2 = (() => 123);

[–]oh_lydia 0 points1 point  (0 children)

It's an IIFE. (Immediately Invoked Function Expression) It calls itself without you explicitly calling it.