What is void(0) in JavaScript?
Table of Contents
When you're working on a website or using JavaScript in general, you might have come across something like this:
JAVASCRIPTjavascript:void(0);
In this post, we'll learn what void(0)
means so the next time you see it, you'll understand what it means.
What is void(0)
?
void(0)
is a JavaScript expression that evaluates to undefined
.
It's used to evaluate an expression without returning a value. It's also used to prevent the default action of an element.
This operator is mostly used in the href
attribute of an anchor tag.
It's used to prevent the browser from following the link. It's also used to prevent the browser from following the link when the link is clicked.
Let's look an example where you might see this:
HTML<a href="javascript:void(0);">Click me</a>
When you click on that link, nothing will happen.
This is useful for when you want to create a link for SEO purpose, but not have it lead to a page.
On the other hand, here is an anchor link with an alert
function instead of void(0)
.
The browser will execute the code since it's not void(0)
.
HTML<a href="javascript:alert('Hello World!');">Click me</a>
When you click on that link, it will show an alert with the text "Hello World!".
Conclusion
In this post, we learned what void(0)
means and how it's used.
If you want a link to essentially do nothing, you can use void(0)
with the javascript:
protocol.
Thanks for reading!
- Getting Started with TypeScript
- Getting Started with Express
- How to Set Up Cron Jobs in Linux
- How to deploy a Deno app using Docker
- Getting Started with Deno
- How to deploy a MySQL Server using Docker
- How to deploy a Node app using Docker
- Learn how to use v-model with a custom Vue component
- How to Scrape the Web using Node.js and Puppeteer
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Getting User Location using JavaScript's Geolocation API
- Getting Started with Moment.js