This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The null keyword refers to the null primitive value, which represents the intentional absence of any object value.
function getVowels(str) {
const m = str.match(/[aeiou]/gi);
if (m === null) {
return 0;
}
return m.length;
}
console.log(getVowels("sky"));
// Expected output: 0
null
The keyword null is a literal for the null value. Unlike undefined, which is a global variable, null is not an identifier but a syntax keyword.
null has the following behaviors:
undefined, accessing any property on null throws a TypeError instead of returning undefined or searching prototype chains.undefined, null is treated as falsy for boolean operations, and nullish for nullish coalescing and optional chaining.typeof null result is "object". This is a bug in JavaScript that cannot be fixed due to backward compatibility.undefined, JSON.stringify() can represent null faithfully.JavaScript is unique to have two nullish values: null and undefined. Semantically, their difference is very minor: undefined represents the absence of a value, while null represents the absence of an object. For example, the end of the prototype chain is null because the prototype chain is composed of objects; document.querySelector() returns null if it doesn't match, because had it matched, the result would be an object. If you are designing an API, you should likely accept null and undefined as equivalent inputs, because many codebases have stylistic rules about when to use null or undefined by default.
null and undefined
When checking for null or undefined, beware of the differences between equality (==) and identity (===) operators, as the former performs type-conversion.
typeof null; // "object" (not "null" for legacy reasons) typeof undefined; // "undefined" null === undefined; // false null == undefined; // true null === null; // true null == null; // true !null; // true Number.isNaN(1 + null); // false Number.isNaN(1 + undefined); // true
| Desktop | Mobile | Server | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | Bun | Deno | Node.js | |
null |
1 | 12 | 1 | 3 | 1 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 | 1 | 1.0.0 | 1.0 | 0.10.0 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/null