Blog/NotesConcept

What is javascript:void(0) and How it Works?

A comprehensive explanation about using javascript:void(0) in javascript. When to use javascript:void(0) and how it works with examples of using it with anchor tag.

Intermediate

Anuj Sharma

Last Updated Jun 15, 2026


What is javascript:void(0) and How it Works?

javascript:void(0) is a very frequently asked javascript interview question for beginners. It's important to understand URI schemes and void() operator in javascript.  In this quick blog, we will cover the details about javascript: URI scheme, void() operator, and their usage with examples along with the use-cases of using javascript:void(0) in the web application  

Table of content

  1. What is javascript:void(0) - A detailed explanation
  2. When should you avoid using javascript:void(0)?
  3. What are the other ways to prevent link navigation?

What is javascript:void(0) - A detailed explanation

what is javascript:void(0)

javascript:void(0) is used to add a fake navigation to the HTML element. It can be used as part of HTML elements which support navigation targets with href property. When the navigation target (href) is invoked on click. It executes the inline javascript code void(0) which returns undefined. It means this code doesn't do anything but prevent navigation.

Example: javascript:void(0) is generally used to prevent navigation when clicking on the anchor tag, and avoid any refresh of the page.

<a href="https://developer.mozilla.org/en-US/docs/Web/URI/Schemes/javascript">
  Navigate to javascript: URI scheme !!
</a>

<a href="javascript:void(0)">
  Do nothing !!
</a>

Understand javascript: and its usage

JavaScript URLs i.e. javascript: is one of the types of URI schemes which decides the type of protocol used while executing the resources. Some other well-known protocol examples include mailto:<mail id> or tel:9675634298

javascript: is generally used to fake the navigation targets, where rather than having an actual URL, it executes a <JS script>. This script can be any legitimate javascript code which will be executed when this navigation target gets triggered.

For example,

// javascript: followed by inline javascript
<a href="javascript:<script>" >
Execute script on click
</a>

Executing alert box on click

<a href="javascript:alert('Hi FrontendGeek !!')">
  Hi FrontendGeek !!
</a>

Executes console.log on click

<a href="javascript:console.log('button clicked !!')">
  Hi FrontendGeek !!
</a>

// console: button clicked !!

Understand void(0) and its usage

void is an operator which executes the expression and returns undefined every time, the input expression doesn't matter in this case. An expression can be any valid javascript expression, It always returns undefined. 

When void(0) or void 0 is used, it executes the expression and returns undefined, and due to this, there is nothing happens when clicking for navigation.

// void(0) or void 0 returns "undefined"

href="javascript:void(0)"
or
href="javascript:void 0"

When should you avoid using javascript:void(0)?

javascript:void(0) usage is not recommended in web development, since it executes the javascript script inline along with the HTML code itself It can lead to bugs if not properly used. It also introduced a few limitations to the web apps, here are a few examples

  1. Hard Accessibility - Using javascript:void(0) leads to poor accessibility support, these inline javascript executions as part of href are not well supported by screen readers.
  2. Poor SEO - Most web crawlers navigate through links to find the relationship between pages. These fake navigation links can break the whole crawling of the bot and can lead to an unindexed web page. 

What are the other ways to prevent link navigation?

There are other ways as well to prevent link navigation that can be used instead of javascript:void(0), since this is not a recommended way of preventing the URL navigation

Using preventDefault()

Event's preventDefault() method can be used to prevent the default navigation behaviour of button or anchor tags. It requires some additional javascript setup to register the event listener against the button click event.

When the event is triggered for the navigation, the preventDefault() method suppresses the default behaviour of the HTML elements like refresh on link click or Form submission. here is an example

<a id="greet" href="">
  Hi FrontendGeek !!
</a>

preventDefault()

const link = document.getElementById('#greet');

link.addEventListener('click', (event) => {
  event.preventDefault();
  
  console.log('Hello clicked !!');
})

Love this Blog? Share it Now!

Help others discover this resource

About the Author

Anuj Sharma

A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) with over 12+ years of experience in the frontend technologies. A frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me  🚀


Learn Next

Featured

20 Most Asked Custom Hooks in React for Interviews

Top 10 React Performance Optimization Techniques25 Top JavaScript Interview Questions for BeginnersHow to create custom useInfiniteScroll Hook in ReactImplement useThrottle Custom Hook In React

Comments

1 comment

Guest User

Please login to comment

0 characters


Marion Pope

marionpope1225@gmail.com

29 Dec, 2025

I'm looking for Siverado 1500.


About the Author

Anuj Sharma

A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) with over 12+ years of experience in the frontend technologies. A frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me  🚀

Share your expertise

Publish a blog or quick notes on topics you know well — your write-up could be the answer someone needs before their next frontend interview.

Build your portfolio

Help the community

Sharpen your skills

Earn goodies

Other Related Blogs

Understand JavaScript Date Object with Examples (for JavaScript Interviews)

Anuj Sharma

Last Updated Jun 15, 2026

Go through different ways to display dates using javascript date object. It covers examples of date object usage to understand the main concepts of javascript date object.

How to Format Phone Number in JavaScript (JavaScript Interview)

Anuj Sharma

Last Updated Jun 15, 2026

Learn the best & quickest way to format phone number in JavaScript with or without country codes. This will help websites to show the phone numbers in a more human-readable format.

Ultimate guide to REST API calls using Fetch: Machine Coding Essential

Vivek Chavan

Last Updated Jun 15, 2026

You will get a clear understanding about working with any rest api and common concepts asked during interviews

Understanding popstate event in Single Page Applications (SPAs)

Vijay Sai Krishna vsuri

Last Updated Feb 21, 2026

A Quick guide about popstate event in JavaScript, If you’ve ever hit the back button in your browser and wondered how your Single-Page Application knows which view to render, this guide is for you.

25+ Top JavaScript Interview Questions and Answers For Beginners

Anuj Sharma

Last Updated Feb 6, 2026

A comprehensive list of important JavaScript Interview Questions and Answers for Beginners with a detailed explanation of the applied JavaScript concept for in-depth understanding.

How to convert RGB to HEX in JavaScript

Anuj Sharma

Last Updated Dec 17, 2025

A quick way to convert RGB to HEX in JavaScript that will help to do the CSS RGB to HEX color conversion programmatically

How to convert HEX to RGB in JavaScript

Anuj Sharma

Last Updated Dec 17, 2025

A quick way to convert HEX to RGB in JavaScript that will help to do the CSS HEX to RGB color conversion programmatically.

5 Different Ways to Reload Page in JavaScript (Frontend Interview)

Anuj Sharma

Last Updated Dec 16, 2025

Explore the 5 most efficient ways to refresh or reload page in JavaScript, similar to location.reload(true), and identify the appropriate use cases to reload window using JS programmatically.

Stay Updated

Subscribe to FrontendGeek Hub for frontend interview preparation, interview experiences, curated resources and roadmaps.

FrontendGeek
FrontendGeek

All in One Preparation Hub to Ace Frontend Interviews. Master JavaScript, React, System Design, and more with curated resources.

Consider Supporting this Free Platform

Buy Me a Coffee

Product

HomeFrontend InterviewFrontend JobsQuestionsNewInterview ExperienceBlogsToolsLeaderboardFrontendGeek Chrome extensionGet the extension on the Chrome Web Store →

© 2026 FrontendGeek. All rights reserved