{"id":32267,"date":"2026-04-17T11:45:00","date_gmt":"2026-04-17T06:15:00","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=32267"},"modified":"2026-04-17T11:44:06","modified_gmt":"2026-04-17T06:14:06","slug":"javascript-void","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/javascript-void\/","title":{"rendered":"JavaScript\u00a0Void Operator: Why Developers Use It in Links"},"content":{"rendered":"<blockquote class=\"wp-block-quote\">\n<p>Developers use <strong>javascript:void(0)<\/strong> in hyperlinks to prevent the browser&#8217;s default action, like navigating to a new page. <\/p>\n<\/blockquote>\n<p>The JavaScript void operator evaluates an expression and discards its return value, which ensures the link returns undefined and prevents the page from reloading or changing when clicked.<\/p>\n<p>In this article, let&#8217;s understand this in the simplest way possible.<\/p>\n<h2 >Getting Started with javascript:void(0) in Links<\/h2>\n<p>When you first learn JavaScript, you may wonder why some links use<strong> javascript:void(0)<\/strong> instead of a normal URL. The reason is simple: it stops the link from trying to navigate to a new page while still letting you run JavaScript. This is useful when you want a link to act like a trigger instead of a regular navigation element.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<div class=\"wp-block-syntaxhighlighter-code \">\n<pre class=\"; title: ; notranslate\" title=\"\">\n&lt;a href=&quot;javascript:void(0)&quot; onclick=&quot;alert(&#039;This is JavaScript running!&#039;)&quot;&gt;Click Me&lt;\/a&gt;\n<\/pre>\n<\/div>\n<p>Here, clicking the link does not reload the page or take you anywhere. Instead, it just runs the code inside the <strong>onclick <\/strong>event. This gives developers control over the action without breaking the user\u2019s flow.<\/p>\n<h2 >How the Void Operator Works<\/h2>\n<p>Let&#8217;s break the word <strong>&#8220;javascript:void(0)&#8221;<\/strong> to understand it&#8217;s work clearly:<\/p>\n<ul >\n<li><strong>javascript:<\/strong> This tells the browser that we are about to run some JavaScript code.<\/li>\n<li><strong>void(0):<\/strong>  This means &#8220;do nothing&#8221; or &#8220;return nothing&#8221;.<\/li>\n<\/ul>\n<p>So when we use <strong>javascript:void(0)<\/strong>, it just asks the link to eat Cadbury 5-Star and do nothing, or we can say that it stops the browser from following the link and so reloading the page.<\/p>\n<h2 >Why Developers Use javascript:void(0) in HTML Links&nbsp;<\/h2>\n<p>Sometimes, developers need links that do more than just navigate to another page. For example, they might want the link to:<\/p>\n<ul >\n<li><strong>Show a hidden section<\/strong> of the page, like a dropdown menu.<\/li>\n<li><strong>Trigger an action<\/strong>, such as to open a popup or to submit a form.<\/li>\n<li><strong>Run a small script<\/strong>, like to change the text on the page or to show a message.<\/li>\n<\/ul>\n<p>In these cases, they don\u2019t actually want the link to take the user to a different web page.<\/p>\n<p>Now, if we run a link using the &#8216;#&#8217; attribute and show an alert after the link is clicked, like this:<\/p>\n<div class=\"wp-block-syntaxhighlighter-code \">\n<pre class=\"; title: ; notranslate\" title=\"\">\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n    &lt;title&gt;Submit Link Example&lt;\/title&gt;\n    &lt;script&gt;\n        function showAlert() {\n            alert(&quot;Your response is submitted&quot;);\n        }\n    &lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;a href=&quot;#&quot; onclick=&quot;showAlert()&quot;&gt;Submit&lt;\/a&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<\/div>\n<p>Then it might cause some problems like:<\/p>\n<ul >\n<li>In HTML, using <strong>#<\/strong> as the <strong>href <\/strong>attribute of an anchor tag <strong>&lt;a&gt;<\/strong> often scrolls to the top of the page and may even cause unwanted errors in user experience.<\/li>\n<li>The link may refresh or reload the page, even though the developer just wants to run some code.<\/li>\n<\/ul>\n<p><strong>Output:<\/strong><\/p>\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"482\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/image-75-1024x482.png\" alt=\"Why Use javascript:void(0) in Links\" class=\"wp-image-32268\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/image-75-1024x482.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/image-75-300x141.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/image-75-768x362.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/image-75.png 1077w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<p>To avoid such problems, a developer can use<strong> javascript:void(0)<\/strong> instead of a button, which tells the browser to prevent the default link behavior and stops unnecessary reload actions. <\/p>\n<h2 >How the Browser Handles the void operator in JavaScript Links<\/h2>\n<p>Suppose we wish to add a link to our webpage. Let&#8217;s say we are adding a link to our website, i.e., <a href=\"https:\/\/codeforgeek.com\/\">https:\/\/codeforgeek.com\/<\/a>. And we want that if the user clicks on the link, an alert is displayed. <\/p>\n<div class=\"wp-block-syntaxhighlighter-code \">\n<pre class=\"; title: ; notranslate\" title=\"\">\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n    &lt;title&gt;Submit Link Example&lt;\/title&gt;\n    &lt;script&gt;\n        function showAlert() {\n            alert(&quot;Welcome to codeforgeek!&quot;);\n        }\n    &lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;a href=&quot;https:\/\/codeforgeek.com\/&quot; onclick=&quot;showAlert()&quot;&gt;www.codeforgeek.com&lt;\/a&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<\/div>\n<p>This code will surely show the alert after the user clicks the link, but it also redirects us to the linked website.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"797\" height=\"480\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/image-76.png\" alt=\"Without javascript:void(0)\" class=\"wp-image-32290\" style=\"width:615px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/image-76.png 797w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/image-76-300x181.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/image-76-768x463.png 768w\" sizes=\"(max-width: 797px) 100vw, 797px\" \/><\/figure>\n<figure class=\"wp-block-video aligncenter\"><video height=\"526\" style=\"aspect-ratio: 870 \/ 526;\" width=\"870\" controls muted src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/24.09.2024_15.01.37_REC.mp4\"><\/video><\/figure>\n<p>Now suppose we don&#8217;t want our webpage to redirect somewhere and prevent it from reloading. To stop this redirecting, we use <strong>javascript:void(0)<\/strong> inside the <strong>href<\/strong> attribute.<\/p>\n<div class=\"wp-block-syntaxhighlighter-code \">\n<pre class=\"; title: ; notranslate\" title=\"\">\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n    &lt;title&gt;Submit Link Example&lt;\/title&gt;\n    &lt;script&gt;\n        function showAlert() {\n            alert(&quot;Welcome to codeforgeek!&quot;);\n        }\n    &lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;a href=&quot; javascript:void(0); https:\/\/codeforgeek.com\/&quot; onclick=&quot;showAlert()&quot;&gt;www.codeforgeek.com&lt;\/a&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<\/div>\n<p><strong>Output:<\/strong><\/p>\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"842\" height=\"465\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/image-77.png\" alt=\"With javascript:void(0)\" class=\"wp-image-32292\" style=\"width:660px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/image-77.png 842w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/image-77-300x166.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/image-77-768x424.png 768w\" sizes=\"(max-width: 842px) 100vw, 842px\" \/><\/figure>\n<figure class=\"wp-block-video aligncenter\"><video height=\"530\" style=\"aspect-ratio: 952 \/ 530;\" width=\"952\" controls muted src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/24.09.2024_15.22.55_REC.mp4\"><\/video><\/figure>\n<h2 >JavaScript Void Example: Handling JS Code Without Reloading the Page<\/h2>\n<p>Let&#8217;s see another example where<strong> javascript:void(0)<\/strong> comes in handy. We will create a Submit link and display a success message on clicking the link without a page refresh.<\/p>\n<div class=\"wp-block-syntaxhighlighter-code \">\n<pre class=\"; title: ; notranslate\" title=\"\">\n&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n    &lt;title&gt;Show More Example&lt;\/title&gt;\n    &lt;style&gt;\n        #extraContent {\n            display: none;\n            margin-top: 10px;\n            background-color: #f9f9f9;\n            padding: 10px;\n            border: 1px solid #ccc;\n        }\n    &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n    &lt;a href=&quot;javascript:void(0)&quot; onclick=&quot;document.getElementById(&#039;extraContent&#039;).style.display=&#039;block&#039;;&quot;&gt;Submit&lt;\/a&gt;\n    \n    &lt;div id=&quot;extraContent&quot;&gt;\n        Your file has been submitted successfully!\n    &lt;\/div&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;\n\n<\/pre>\n<\/div>\n<p>In this code, <strong>javascript:void(0)<\/strong> is used in the <strong>href<\/strong> to stop the page from reloading or navigating anywhere when the <strong>&#8220;Submit&#8221;<\/strong> link is clicked. Instead, the <strong>onclick<\/strong> event is used to display hidden content (<strong>#extraContent<\/strong>). This approach treats<strong> javascript:void(0)<\/strong> as a pseudo-url in JS programming, letting developers trigger code without reloading the page, and errors can be debugged directly in the browser console.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<figure class=\"wp-block-video aligncenter\"><video height=\"536\" style=\"aspect-ratio: 856 \/ 536;\" width=\"856\" controls muted src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/09\/24.09.2024_15.48.00_REC.mp4\"><\/video><\/figure>\n<h2 >Handling User Interaction with JavaScript Void<\/h2>\n<p>A link using<strong> javascript:void(0)<\/strong> can be styled with CSS to look like a button or checkbox. Developers can also attach events for keypress, keyboard, or mouse actions so the same interaction works consistently across devices.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<div class=\"wp-block-syntaxhighlighter-code \">\n<pre class=\"; title: ; notranslate\" title=\"\">\n&lt;a href=&quot;javascript:void(0)&quot; \n   onclick=&quot;toggleCheck()&quot; \n   onkeypress=&quot;toggleCheck()&quot; \n   style=&quot;display:inline-block; padding:8px; background:#007bff; color:#fff; cursor:pointer;&quot;&gt;\n   Toggle Checkbox\n&lt;\/a&gt;\n\n&lt;input type=&quot;checkbox&quot; id=&quot;checkBox&quot; style=&quot;margin-left:10px;&quot;&gt;\n&lt;script&gt;\n    function toggleCheck() {\n        const box = document.getElementById(&quot;checkBox&quot;);\n        box.checked = !box.checked;\n    }\n&lt;\/script&gt;\n\n<\/pre>\n<\/div>\n<p>Here we made a link act like a checkbox. It responds to both mouse clicks and keyboard keypresses. With a little CSS, it looks like a button, but <strong>javascript:void(0)<\/strong> prevents page reloads.<\/p>\n<h2 >Accessibility and Semantic Considerations<\/h2>\n<p>While<strong> javascript:void(0)<\/strong> is useful, using it excessively may confuse screen readers. For more semantic HTML, developers often prefer proper buttons that describe the contents of the page. This improves accessibility and ensures a better user experience.<\/p>\n<p><strong>Example (better alternative):<\/strong><\/p>\n<div class=\"wp-block-syntaxhighlighter-code \">\n<pre class=\"; title: ; notranslate\" title=\"\">\n&lt;button onclick=&quot;alert(&#039;Form submitted!&#039;)&quot;&gt;Submit&lt;\/button&gt;\n<\/pre>\n<\/div>\n<p>Instead of forcing a link to behave like a button, just use a <strong>&lt;button&gt;<\/strong>. This is easier for assistive technology like screen readers and keeps your HTML semantic and user-friendly.<\/p>\n<h2 >In Short<\/h2>\n<p>In short, <strong>javascript:void(0)<\/strong> is a handy JavaScript keyword that stops a link from trying to load a URL or redirect. The void operator evaluates an expression and returns undefined, letting developers discard any unwanted return value. <\/p>\n<p><strong>Example Recap:<\/strong><\/p>\n<div class=\"wp-block-syntaxhighlighter-code \">\n<pre class=\"; title: ; notranslate\" title=\"\">\n&amp;lt;a href=&quot;javascript:void(0)&quot; onclick=&quot;console.log(&#039;Executed safely!&#039;)&quot;&gt;Run Code&amp;lt;\/a&gt;\n\n<\/pre>\n<\/div>\n<p>This tiny snippet shows how<strong> javascript:void(0)<\/strong> keeps you in control of your link, executes code, but doesn\u2019t reload or disrupt the page.<\/p>\n<h2 >Reference<\/h2>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/1291942\/what-does-javascriptvoid0-mean\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/1291942\/what-does-javascriptvoid0-mean<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Developers use javascript:void(0) in hyperlinks to prevent the browser&#8217;s default action, like navigating to a new page. The JavaScript void operator evaluates an expression and discards its return value, which ensures the link returns undefined and prevents the page from reloading or changing when clicked. In this article, let&#8217;s understand this in the simplest way [&hellip;]<\/p>\n","protected":false},"author":73,"featured_media":35304,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[9],"tags":[],"class_list":["post-32267","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-js"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/08\/Void-Operator-in-JavaScript.png",1280,720,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/08\/Void-Operator-in-JavaScript-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/08\/Void-Operator-in-JavaScript-300x169.png",300,169,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/08\/Void-Operator-in-JavaScript-768x432.png",768,432,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/08\/Void-Operator-in-JavaScript-1024x576.png",1024,576,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/08\/Void-Operator-in-JavaScript.png",1280,720,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/08\/Void-Operator-in-JavaScript.png",1280,720,false]},"uagb_author_info":{"display_name":"Ninad Pathak","author_link":"https:\/\/codeforgeek.com\/author\/ninad\/"},"uagb_comment_info":0,"uagb_excerpt":"Developers use javascript:void(0) in hyperlinks to prevent the browser&#8217;s default action, like navigating to a new page. The JavaScript void operator evaluates an expression and discards its return value, which ensures the link returns undefined and prevents the page from reloading or changing when clicked. In this article, let&#8217;s understand this in the simplest way&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/32267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/73"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=32267"}],"version-history":[{"count":1,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/32267\/revisions"}],"predecessor-version":[{"id":37710,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/32267\/revisions\/37710"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/35304"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=32267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=32267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=32267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}