-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
  • 6,363
  • 1
  • 30
  • 34
  • 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
  • 857,932
  • 118
  • 1,152
  • 1,264
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
  • 32,084
  • 8
  • 49
  • 92