Skip to main content

Command Palette

Search for a command to run...

VOID in JavaScript

void(0) in javascript

Updated
VOID in JavaScript
C

CapsCode: Empowering Innovation Through Education and Technology

CapsCode is a pioneering edtech and web development company committed to transforming the lives of students and businesses alike. Founded with the vision of creating a brighter, tech-driven future, CapsCode empowers students by providing world-class coding education while enabling businesses to achieve their goals with cutting-edge IT solutions.

For Students At CapsCode, we believe that coding is the language of tomorrow. Through engaging and industry-relevant courses, personalized mentoring, and hands-on workshops, we help students develop: Problem-solving skills: Preparing them for real-world challenges. Technical expertise: Equipping them with practical knowledge of coding and web development. Confidence in technology: Making them future-ready for careers in IT and beyond.

We foster an environment where students not only learn to code but also learn to innovate, collaborate, and think critically.

For Businesses CapsCode offers bespoke IT solutions tailored to the unique needs of businesses. From responsive web development to advanced digital marketing strategies, our services are designed to help companies grow their online presence and streamline their operations. Our key offerings include: Custom website and software development E-commerce platform creation Lead generation and digital marketing Professional email and Google Business setup

We act as partners in innovation, helping businesses leverage technology to reach new heights.

Our Mission Our mission is to empower individuals and organizations by bridging the gap between technology and accessibility. We aim to inspire a new generation of coders while helping businesses thrive in a competitive digital landscape.

Why Choose CapsCode? Over 9 years of IT industry expertise. A blend of education and professional IT services under one roof. A commitment to fostering growth, innovation, and success.

Join us in shaping the future—whether you're a student eager to unlock your potential or a business ready to embrace the possibilities of technology.

Learn. Build. Grow. With CapsCode.

Hello Devs,

If you are a web developer then you must know this concept of JavaScript i.e VOID / VOID(0) In this article i am going to tell you everything about VOID operator in JavaScript. So without wasting any time, lets get into the article

Table of contents

What is VOID ?

void: void means completely empty/ or nothing NOTE: null and void are different

⬆ Back to Top

What VOID operator do ?

void operator in JS evaluates the given expression and then returns undefined

syntax: void(2+3) //this evaluated 2+3 and return undefined: lets prove this void(console.log(2+3))//this will print 5 but return undefined

⬆ Back to Top

What is the use of VOID operator ?

use of void(0) in <a> tag Void(0) is used to prevent the page from refreshing and parameter "zero" is passed while calling. Void(0) is used to call another method without refreshing the page.

⬆ Back to Top

When to use Javascript void ?

We can use Javascript void(0) when we do not want the browser to load a new page or refresh the same page​ when a link is clicked. Instead, we want the browser to perform the Javascript attached to that link.

⬆ Back to Top

why to use void ?

<a href="" onclick="alert('The page will reload!!!')">Click Here to display alert!</a>

Click Here to display alert! this will show alert and then reload the page.

<a href="javascript:void(0);" onclick="alert('The page will not reload.')">Click Here to display alert!</a>

Click Here to display alert! this will alert but will not reload the page.

We can also run any JS code without reloading or refreshing the page.

<a id='link' href="javascript:void(document.querySelector('#link').style.color = 'green')">Link</a>

Another use case of links with the javascript:void(0) reference is that sometimes, a link may run some JavaScript code in the background, and navigating may be unnecessary. In this case, the expressions would be used as the arguments passed to void.

A common usage of JavaScript:Void(0) is with hyperlinks.

Sometimes, you may need to call some JavaScript from within a link. Normally, when you click a link, the browser loads a new page or refreshes the same page (depending on the URL specified). But you probably don't want this to happen if you've attached some JavaScript to that link.

To prevent the page from unnecessary refreshing, you could use JavaScript:void(0).

some additional examples:

<a href="#" ondblclick="alert('Well done!')">Double Click Me!</a>

Double Click Me! --page refreshed as soon you clicked the link

<a href="javascript:void(0);" ondblclick="alert('Well done!')">Double Click Me!</a>

Double Click Me! --this don't

⬆ Back to Top

Thank you for reading this far. This is a brief introduction of VOID in JS . If you find this article useful, like and share this article. Someone could find it useful too. If you find anything technically inaccurate please feel free to comment below. Now you can also play around the objects in JS.

Hope its a nice and informative read for you. VISIT https://www.capscode.in/#/blog TO LEARN MORE...

Thanks, @capscode