What is the use of ()(parenthesis) brackets in accessing a function in JavaScript?


The ()(parenthesis) brackets play an important role in accessing a function. Accessing a function without () will return the function definition instead of the function result. If the function is accessed with () then the result can be obtained.

Without ()

Example

In the following example, the function is accessed without () so the function definition is returned instead of the result as shown in the output.

Live Demo

<html>
<body>
<script>
   function toCelsius(f) {
      return (5/9) * (f-32);
   }
   document.write(toCelsius);
</script>
</body>
</html>

Output

function toCelsius(f) { return (5/9) * (f-32); }


With ()

Example

In the following example, the function is accessed with () so instead of the function definition, the result is displayed as shown in the output. 

Live Demo

<html>
<body>
<script>
   function toCelsius(f) {
      return (5/9) * (f-32);
   }
   document.write(toCelsius(208));
</script>
</body>
</html>

Output

97.77777777777779
raja
Published on 29-Jul-2019 14:40:53
Advertisements