-1

I recently saw href='javascript:;'. What's the difference between:

<a href='javascript:;'

vs:

<a href='javascript:void(0);'

When to use first one, and when to use second?

yaya
  • 7,675
  • 1
  • 39
  • 38
  • Does this answer your question? [What does "javascript:void(0)" mean?](https://stackoverflow.com/questions/1291942/what-does-javascriptvoid0-mean) – Kamuran Sönecek Jul 23 '20 at 14:03
  • @KamuranSönecek i know what `javascript:void(0)` means. i'm asking about how it's different from `javascript:;` – yaya Jul 23 '20 at 14:07

2 Answers2

2

One is a JavaScript program with no statements which resolves as undefined, the other uses void to explicitly resolve as undefined. There is little to choose between them.

Never use either.

Only use a link if you are going to link to somewhere.

If you want a UI control that people can click on to trigger some JavaScript, use a <button type="button">.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

There is no practical difference. Both result in the expression evaluating to undefined.

void / void() is just a shortcut to get to undefined, as is an empty expression terminating with an immediate semicolon.

Mitya
  • 33,629
  • 9
  • 60
  • 107