Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the use of ()(parenthesis) brackets in accessing a function in JavaScript?
The parentheses () are crucial for function invocation in JavaScript. Accessing a function without parentheses returns the function reference, while using parentheses calls the function and returns its result.
Function Reference vs Function Call
When you write a function name without parentheses, JavaScript treats it as a reference to the function object. With parentheses, JavaScript executes the function.
Without Parentheses - Function Reference
Accessing a function without parentheses returns the function definition itself, not the result:
<html>
<body>
<script>
function toCelsius(f) {
return (5/9) * (f-32);
}
document.write(toCelsius);
</script>
</body>
</html>
function toCelsius(f) { return (5/9) * (f-32); }
With Parentheses - Function Call
Adding parentheses executes the function and returns the calculated result:
<html>
<body>
<script>
function toCelsius(f) {
return (5/9) * (f-32);
}
document.write(toCelsius(208));
</script>
</body>
</html>
97.77777777777779
Practical Use Cases
Function references are useful for:
- Callback functions: Passing functions as arguments
- Event handlers: Assigning functions to events
- Function assignment: Storing functions in variables
<html>
<body>
<script>
function greet() {
return "Hello World!";
}
// Function reference - stored in variable
let myFunction = greet;
document.write("Reference: " + myFunction + "<br>");
// Function call - executes and returns result
document.write("Call: " + greet());
</script>
</body>
</html>
Reference: function greet() { return "Hello World!"; }
Call: Hello World!
Key Differences
| Syntax | Returns | Use Case |
|---|---|---|
functionName |
Function reference | Callbacks, assignments |
functionName() |
Function result | Execute function logic |
Conclusion
Parentheses are essential for function execution in JavaScript. Without them, you get the function reference; with them, you invoke the function and get its return value. Understanding this distinction is fundamental for JavaScript programming.
