• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

void keyword in javascript

LumbergTech

Diamond Member
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.
 
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?
 
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.
 
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.
 
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:
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.
 
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.
 
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.
 
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.
 
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:
Back
Top