Call JavaScript Function without Parentheses

We are taught to call a JavaScript function with parenthese, e.g. alert(‘test’);, but believe it or not, there are at least 6 solutions to invoke a JavaScript function without parentheses ‘()’.  There is no valid reason for doing this.  I share these solutions just for fun.  🙂

Solution 1. As a constructor

With the JavaScript new keyword you can invoke a function without parentheses:

function f() {
new f;

According to MDN on the new oprator, you can call a constructor without parenthese.

new constructor[([arguments])]

Solution 2. As toString or valueOf implementation

toString and valueOf are special methods: they get called implicitly when a conversion is necessary:

var obj = {
    toString: function() {
         return 'hello';

'' + obj; // concatenation forces cast to string and call to toString.

You could (ab)use this pattern to call f without parentheses:

'' + { toString: f };

or with valueOf:

+{ valueOf: f };

You could take the previous idea to override the valueOf method on the Function prototype.  See more about this in this blog article: “Calling methods in Javascript, without really calling them”

Solution 3. As Generator

You could define a generator function (with *), which returns an iterator. You can call it with the spread operator or for...of syntax.

First we need a generator variant of the original f function:

function* f_gen() {

And then we call it without parentheses:

[...{ [Symbol.iterator]: f_gen }];

Normally generators would have a yield keyword somewhere, but it is not needed for the function to get called.

The last statement invokes the function, but that could also be done with destructuring:

[,] = { [Symbol.iterator]: f_gen };

or a for ... of construct:

for ({} of { [Symbol.iterator]: f_gen });

Note that you can do the above with the original greet function as well, but it will trigger an exception in the process, after greet has been executed (tested on FF and Chrome). You could manage the exception with a try...catch block.

Solution 4. As Getter or Setter

You can use getters and setters.

var h = {
  get ello () {

Run this script just with:

h.ello  // Fires up alert "world"

We can even do arguments!

var h = {
  set ello (what) {
    alert("Hello " + what);

h.ello = "world" // Fires up alert "Hello world"

You can also define global functions that can be run without parenthesis:

window.__defineGetter__("hello", function() { alert("world"); });
hello;  // Fires up alert "world"

And with arguments:

window.__defineSetter__("hello", function(what) { alert("Hello " + what); });
hello = "world";  // Fires up alert "Hello world"

Solution 5. As Tag Function

With ES6 you can call a function passing it a template literal with this syntax:


See “Tagged Template Literals”. Normally you would not pass an empty literal like here, but as example of a parentheses-less call, it will do.

Solution 6. As Proxy Handler

In ES6, you can define a proxy:

var proxy = new Proxy({}, { get: greet } );

And then reading any property value will invoke greet:

proxy._; // even if property not defined, it still triggers greet

There are many variations of this. One more example:

var proxy = new Proxy({}, { has: greet } );

1 in proxy; // triggers greet

Leave a Reply

Be the First to Comment!

Notify of