0

I have more than a few doubts regarding javascript:void(0). the following are my doubts.

  • I know it returns "undefined" but what is the significance of it. Or in other words, at what sitiation or circumstance do we use javascript:void(0); what exactly does the programmer want when he uses javascript:void(0)
  • I'm asking this question because i dont have a clear understanding of javascript:void(0) and it might be stupid. But what would happen if i use javascript:myFunction("some argument") instead. like for example <a href="javascript:myFunction("args")"></a>

please focus more on the second part.

1 Answers1

0

say if you put

<a href="#" onclick="some_function">Hii</a>

and when you will click the url, you will see # getting appended to the url

but if we put href="javascript:void(0);" that means we are calling a void javascript function.

It wont append # at the end of url.

The void operator is often used merely to obtain the undefined primitive value, usually using “void(0)” (which is equivalent to “void 0”). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).

Also if you put

<a href="javascript:myFunction('args')">Huii</a>

then if u see in console it will tell you myFunction() is not defined.

Saswat
  • 12,320
  • 16
  • 77
  • 156