View Full Version : Calling a function - with or without brackets / either way?

10-05-2011, 03:14 PM
<input type="button" value="Click!" id="001" />

function one() {

var varname=document.getElementById("001");

I am not sure if the last line is this example is correct. Should it not be:


Or is it ok to write it either way?

Philip M
10-05-2011, 03:22 PM
No, the meaning is quite different.

To call (use, execute, invoke) the function you must include the () brackets.
If you omit the brackets, you don't actually call the function. Instead you get a reference to the function object. (Whenever you use a function name without parentheses, it acts like a regular variable).

<script type = "text/javascript">

function a() {
alert ("Hello World");


var x = a;
alert (x);


You should not delete your posts as they may be of value to someone else. Also as a courtesy to anyone who has troubled to answer them.

"When I was a kid I used to pray every night for a new bike. Then I realised that The Lord doesn't work that way, so I stole one and asked him to forgive me."