this will do the trick for you

if (!!val) {
    alert("this is not null")
} else {
    alert("this is null")
}
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Nullish coalescing operator '??'
The nullish coalescing operator is written as two question marks ??. As it treats null and undefined similarly, we’ll use a special term here, in this article. For brevity, we’ll say that a value is “defined” when it’s neither null ...
Discussions

Good way to check for variable being not null and not undefined.
There are some style guides that basically say you always should use ===, but you can use == in order to check for null or undefined at the same time. So you could do the following: if (value != null) { // This will run if `value` is not `null` and not `undefined`. } More on reddit.com
🌐 r/javascript
56
32
October 20, 2016
javascript - Opposite of nullish coalescing operator - Stack Overflow
Nullish coalescing operator allows assigning a variable if it's not null or undefined, or an expression otherwise. a = b ?? other It is an improvement over previously used || because || will also ... More on stackoverflow.com
🌐 stackoverflow.com
[AskJS] Nullish Check in conditional
value == null only matches null and undefined, not any other falsy values. This is the only time you should use == over ===. More on reddit.com
🌐 r/javascript
23
7
August 16, 2024
Thoughts on the Null Coalescing (??) operator precedence?
When something relatively new comes out, it's common for the first few games in town to foul up the artistry. Wirth is a towering intellect in this field, but he screwed up the precedence tables in Pascal. Experience will highlight mistakes, and then it's eventually time to design a new language. ?? clearly goes after function-call and field-access, but before arithmetic. The field-access counterpart .? should be on the same level as non-null field-access .. More on reddit.com
🌐 r/ProgrammingLanguages
11
31
April 30, 2024
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing
Nullish coalescing operator (??) - JavaScript - MDN Web Docs
... const count = 0; const text = ""; const qty = count || 42; const message = text || "hi!"; console.log(qty); // 42 and not 0 console.log(message); // "hi!" and not "" The nullish coalescing operator avoids this pitfall by only returning the second operand when the first one evaluates to ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Optional_chaining
Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
1 month ago - By using the ?. operator instead of just ., JavaScript knows to implicitly check to be sure obj.first is not null or undefined before attempting to access obj.first.second.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Nullish_coalescing_assignment
Nullish coalescing assignment (??=) - JavaScript | MDN
In fact, if x is not nullish, y is not evaluated at all. ... You can use the nullish coalescing assignment operator to apply default values to object properties.
🌐
TypeScript
typescriptlang.org › docs › handbook › release-notes › typescript-2-0.html
TypeScript: Documentation - TypeScript 2.0
A new ! post-fix expression operator may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact. Specifically, the operation x! produces a value of the type of x with null and undefined excluded. Similar to type assertions of the forms <T>x and x as T, the ! non-null assertion operator is simply removed in the emitted JavaScript code...
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-null-check
How to Check for Null in JavaScript | Built In
The double equality == operator confirms the absence of any value and does not directly check for null. But one way to check for null in JavaScript is to check if a value is loosely equal to null using the double equality == operator:
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-check-if-a-variable-is-not-null-in-javascript
How to check if a Variable Is Not Null in JavaScript ? | GeeksforGeeks
May 1, 2025 - One of the most direct and simple ways to check if a variable is not null is to use the strict equality operator !==. This operator checks if the value is strictly not equal to null, ensuring there’s no type coercion.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-check-for-null-in-javascript
JS Check for Null – Null Checking in JavaScript Explained
November 7, 2024 - Null is a primitive type in JavaScript. This means you are supposed to be able to check if a variable is null with the typeof() method. But unfortunately, this returns “object” because of an historical bug that cannot be fixed. let userName ...
🌐
freeCodeCamp
freecodecamp.org › news › javascript-advanced-operators
Advanced JavaScript Operators – Nullish Coalescing, Optional Chaining, and Destructuring Assignment
January 4, 2024 - This is why the optional chaining operator was created. The operator returns either the value of the property, or undefined when the property is null or undefined. To use the operator, just add a question mark in front of the dot . notation:
🌐
Chris Pietschmann
pietschsoft.com › post › 2008 › 10 › 14 › javascript-gem-null-coalescing-using-the-or-operator
JavaScript: Null Coalesce using the || Operator | Chris Pietschmann
October 14, 2008 - Since JavaScript returns a boolean value of true when your looking at a variable that is not set to null or undefined, you can use the || (or) operator to do null coalescing. Basically, as long as the first value is not null or undefined it’s ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-check-for-null-values-in-javascript
How to check for null values in JavaScript ? - GeeksforGeeks
July 23, 2025 - That is because a variable that has been declared but not assigned any value is undefined, not null. ... By this operator, we will learn how to check for null values in JavaScript by the (===) operator.
🌐
Medium
medium.com › nerd-for-tech › javascript-better-way-to-check-for-nullish-values-395d08ffe2b1
JavaScript — Better way to check for Nullish values | by Apoorv Tyagi | Nerd For Tech | Medium
December 4, 2021 - We have seen the nullish coalescing operator is really useful when you only care about the null or undefined value for any variable. The whole point of Nullish Coalescing Operator is to distinguish between nullish (null, undefined) and falsey ...
🌐
Scaler
scaler.com › home › topics › javascript program to check for null
JavaScript Program to Check for Null - Scaler Topics
May 4, 2023 - Inside the if condition, we have ... true and the value of !x is also true. ... In this approach, we will use the equality operator '==' to check if the variable contains null or not....
🌐
JavaScript Tutorial
javascripttutorial.net › home › an essential guide to javascript null
An Essential Guide to JavaScript null
September 29, 2020 - const rect = null; const square ... it’s assigned to an object. To check if a value is not null, you use the strict inequality operator (!==):...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript - MDN Web Docs
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
🌐
Medium
oopanpan.medium.com › nullish-coalescing-operator-in-javascript-c490260433a4
Nullish Coalescing Operator in JavaScript | by Pan Li | Medium
August 15, 2021 - Let me introduce you the nullish ... that we had with the logical OR operator “||”. ... If operandOne is NOT null or undefined then the result will be operandOne....
🌐
Medium
medium.com › @maxwellarmah01 › understanding-javascript-comparisons-why-0-is-null-and-not-null-372d84d65284
Understanding JavaScript Comparisons: Why 0 is null and not null | by Maxwell Armah | Medium
June 10, 2024 - The equality operator (`==`), also known as loose equality, follows a different set of rules for type conversion: 1. Type Conversion for Loose Equality: — When comparing a number to `null`, JavaScript treats `null` as a special case. — `null` only loosely equals `undefined` and no other value. It does not convert `null` to `0` as it does in the case of relational comparisons.