void keyword in javascript

LumbergTech

Diamond Member
Sep 15, 2005
3,622
1
0
I am struggling to understand the point of the void keyword in javascript. I see that you can evaluate an expression to "undefined" with it...but why would you want to do that? I read about 5 explanations and none of them gave an example of where I would want to actually do this.
 

Jaydip

Diamond Member
Mar 29, 2010
3,691
21
81
I can see myself using it in client side scripts, but there are better alternatives.
 

LumbergTech

Diamond Member
Sep 15, 2005
3,622
1
0
I can see myself using it in client side scripts, but there are better alternatives.

Can you explain to me the thinking process behind when you would choose to use this?

When would you need to evaluate an expression but have the result be undefined?
 

Jaydip

Diamond Member
Mar 29, 2010
3,691
21
81
Can you explain to me the thinking process behind when you would choose to use this?

When would you need to evaluate an expression but have the result be undefined?

The easiest one comes to mind when issuing a alert message to the user.I want the alert expression to be evaluated without loading it in my document.From what I have seen though it is mostly used for bookmarks.
 

LumbergTech

Diamond Member
Sep 15, 2005
3,622
1
0
The easiest one comes to mind when issuing a alert message to the user.I want the alert expression to be evaluated without loading it in my document.From what I have seen though it is mostly used for bookmarks.

Pardon my ignorance, but can you explain why you wouldn't want to load it in your document? I appreciate your responses.
 

Jaydip

Diamond Member
Mar 29, 2010
3,691
21
81
Pardon my ignorance, but can you explain why you wouldn't want to load it in your document? I appreciate your responses.

Just test this in a browser
Code:
<a href="javascript:void(alert('Test'))">Test</a>
Without the void keyword it loads the alert('Test') in the document itself.
 
Last edited:

GregGreen

Golden Member
Dec 5, 2000
1,681
3
81
I was curious about this because I have never once used the void keyword in my JS and couldn't think of any good reason to use it, so I went and checked all the JS books I have and could only find one reference.

From Douglas Crockford's, JavaScript: The Good Parts on page 114:

"In many languages, void is a type that has no values. In JavaScript, void is an operator that takes an operand and returns undefined. This is not useful, and it is very confusing. Avoid void."

Obviously you shouldn't take anyone's opinion without thinking about it yourself, but I can't say he's wrong here.
 

KB

Diamond Member
Nov 8, 1999
5,401
386
126
I have never once used void in any of my code. It is an unnecessary 4 letters and in javascript reducing the size is a big deal.

It seems to be added for one purpose as described by Jaydip and mentioned here:
"This operator allows inserting expressions that produce side effects into places where an expression that evaluates to undefined is desired."

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/void

However putting inline javascript in a link is bad practice, so it negates the need for void.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
Not familiar with JS, but I always used VOID functions in C++ to repeatedly do a certain task I didn't need a return for.
 

Jaydip

Diamond Member
Mar 29, 2010
3,691
21
81
I have never once used void in any of my code. It is an unnecessary 4 letters and in javascript reducing the size is a big deal.

It seems to be added for one purpose as described by Jaydip and mentioned here:
&quot;This operator allows inserting expressions that produce side effects into places where an expression that evaluates to undefined is desired.&quot;

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/void

However putting inline javascript in a link is bad practice, so it negates the need for void.

Spot on.At this moment it is mostly used for academic purposes.
 

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
Back in the day we used href="javascript:void(0)" to make an href that does nothing.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
Back in the day we used href="javascript:void(0)" to make an href that does nothing.

I define an onclick handler for the anchor. The first thing the onclick handler does is disable propogation so the href doesn't actually do anything. Just in case, I set href to "#".

Not sure what's better. When you hover over my anchor, the target appears to be "#". When you hover over your anchor, your target appears to be"javascript:void(0)". Both are dumb. My way is shorter.

Sure I could use text and define a bunch of CSS to make it look and act like a link, but that's actually non-trivial to do.
 
Last edited:
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |