Difference between Func() , ()=>Func() , {Func} , ()=>Func in JS
Jaydeep Pipaliya

Jaydeep Pipaliya @itsjp

Joined:
Mar 12, 2024

Difference between Func() , ()=>Func() , {Func} , ()=>Func in JS

Publish Date: Apr 18 '24
4 5

Here's a breakdown of what each syntax typically means in JavaScript, which seems the most likely context for these snippets:

expression

1. Function():

  • This is a direct call to a function named Function. If Function is a predefined function in your code, this syntax immediately invokes it and evaluates to the result of the function call.

2. () => Function():

  • This is an arrow function that returns the result of calling Function() when it is invoked. Arrow functions are a feature of ES6 (ECMAScript 2015) and provide a concise syntax to write function expressions. They do not have their own this, arguments, super, or new.target bindings, which means they are often used when you do not need to access these values, or you want to maintain the lexical value of this from the surrounding code.

3. {Function}:

  • In a JavaScript context, this typically represents an object literal containing a shorthand property Function. If Function is a variable in the scope where this object is defined, the property's name will be Function and its value will be the value of the Function variable. This is not a function call or a function definition by itself.

4. () => Function

  • This arrow function returns the function Function itself, not the result of calling Function(). This is useful when you need to pass a function as a callback without executing it immediately.

Here’s a practical example to illustrate these differences:

function example() {
  return 'Hello, world!';
}

// Invokes the function and prints the return value
console.log(example());  // Output: "Hello, world!"

// Defines an arrow function that, when called, will invoke example()
const arrowFunctionCall = () => example();
console.log(arrowFunctionCall());  // Output: "Hello, world!"

// Creates an object with a property example that holds a function
const objectWithFunction = {example};
console.log(objectWithFunction.example());  // Output: "Hello, world!"

// Defines an arrow function that returns the function itself
const arrowFunction = () => example;
console.log(arrowFunction()());  // Output: "Hello, world!"

Enter fullscreen mode Exit fullscreen mode

In this code, example() *calls the function directly, *() => example() defines a function that returns the function example, {example} is an object with a property example that is the function, and **() => example **defines an arrow function that returns the function example itself.

Comments 5 total

  • Jaydeep Pipaliya
    Jaydeep PipaliyaApr 18, 2024

    how can i write code that render with js syntax , for now it is just black and white

    • Jon Randy 🎖️
      Jon Randy 🎖️Apr 18, 2024

      Use three backticks followed by js at the start of your code block, and end the code with three backticks.

      ```js
      // your code here
      ```

  • Jon Randy 🎖️
    Jon Randy 🎖️Apr 18, 2024

    The examples here are not great, since defining a function called Function is a pretty bad idea, as it is normally the constructor for a function (although this is not often used):

    const myFunction = new Function("console.log('Hello world!')")
    myFunction()  // Hello world!
    
    Enter fullscreen mode Exit fullscreen mode

    Also, number 3 is not quite correct - it completely depends upon the context in which {Function} is encountered. If encountered where a statement is valid, it will be interpreted as a block statement containing just a reference to the Function. Otherwise, as you state, it will be an object containing a single property named for the function, containing that function. This can be checked by using the console:

    // type this into the console first to define a function
     const myFunc = function() {}
    
    // type this into the console to see what {myFunc} gives in
    // a context where a statement is not valid
     ({myFunc}) 
    // console will show: Object { myFunc: myFunc() }
    
    // now type { myFunc } into a context where a statement IS valid
     {myFunc} 
    // console will show: function myFunc()
    
    Enter fullscreen mode Exit fullscreen mode
  • Shyam raj
    Shyam rajApr 18, 2024

    Wow.. Interesting❤️

Add comment