Facebook Pixel

What is void 0 in JavaScript and why is it used?

void 0 always evaluates to undefined and cannot be shadowed. It is used in old code and minified output to safely get undefined when the global property might have been reassignable. In modern JS (ES5+), x === undefined is sufficient.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in How to Check for undefined in JavaScript

Use strict equality: x === undefined. This checks both type and value. It is the canonical, safe way to check if a declared variable has the value undefined.

Because !x also catches null, 0, '', false, and NaN. If you only want to check for undefined, use === undefined. Use !x only when you want to catch all falsy values.

Use typeof. typeof an undeclared variable returns 'undefined' without throwing. For example: if (typeof someGlobal !== 'undefined') is safe for feature detection.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.