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
! operator in typescript after object method - Stack Overflow
@Rishav yes, you're right about the (javascript) optional chaining operator, and you're also correct about the (typescript) non-null assertion operator. but they also mean completely different things. The first says "i'm not sure, it can be null, but if not go ahead and..." but the later says ... 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
🌐
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.
🌐
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:
🌐
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...
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 ...
🌐
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 (!==):...
🌐
Medium
medium.com › @gabrielairiart.gi › advanced-javascript-use-of-nullish-coalescing-and-optional-chaining-and-for-efficient-coding-7d1d3fe3eedf
Advanced JavaScript: Use of Nullish Coalescing ?? and Optional Chaining and ?. for Efficient Coding | by Gabriela Iriart | Medium
March 22, 2024 - Choosing between the Nullish Coalescing Operator (??) and the Optional Chaining Operator (?.) ultimately hinges on the particular situation at hand: Employ ?? when you need to assign a default value to a variable that might be null or undefined.
🌐
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.
🌐
daily.dev
daily.dev › home › blog › webdev › nullish coalescing operator (??) in javascript - what is it and how to use it?
Nullish Coalescing Operator (??) In JavaScript - What Is It And How To Use It? | daily.dev
August 24, 2020 - The Nullish Coalescing Operator returns the right-hand side value when the left-side one is `undefined` or `null`. Using the OR (`||`) operator, you can run into troubles with falsy values like 0 and empty strings.
Top answer
1 of 3
170

It's called the "Non-null assertion operator" and it tells the compiler that x.getY() is not null.

It's a new typescript 2.0 feature and you can read about it in the what's new page, here's what it says:

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 x and x as T, the ! non-null assertion operator is simply removed in the emitted JavaScript code.

Copy// Compiled with --strictNullChecks
function validateEntity(e?: Entity) {
    // Throw exception if e is null or invalid entity
}

function processEntity(e?: Entity) {
    validateEntity(e);
    let s = e!.name;  // Assert that e is non-null and access name
}

Edit

There's an issue for documenting this feature: Document non-null assertion operator (!)

2 of 3
45

Non-null assertion operator: !

  • You tells the TS compiler that the value of a variable is not null | undefined
  • Use it when you are in possession of knowledge that the TS compiler lacks.

Here is a trivial example of what it does:

Copylet nullable1: null | number;
let nullable2: undefined | string;

let foo  = nullable1! // type foo: number
let fooz = nullable2! // type fooz: string

It basically removes null | undefined from the type


When do I use this?

Typescript is already pretty good at inferring types for example using typeguards:

Copylet nullable: null | number | undefined;

if (nullable) {
    const foo = nullable; // ts can infer that foo: number, since if statements checks this
}

However sometimes we are in a scenario which looks like the following:

Copytype Nullable = null | number | undefined;

let nullable: Nullable;

validate(nullable);

// Here we say to ts compiler:
// I, the programmer have checked this and foo is not null or undefined
const foo = nullable!;  // foo: number

function validate(arg: Nullable) {
    // normally usually more complex validation logic
    // but now for an example
    if (!arg) {
        throw Error('validation failed')
    }
}

My personal advice is to try to avoid this operator whenever possible. Let the compiler do the job of statically checking your code. However there are scenarios especially with vendor code where using this operator is unavoidable.