Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does exclamation point after variable mean in JavaScript? [duplicate]

I've seen in some React/TypeScript implementations such as :

ref={ ref => this.container = ref! }

What does the exclamation point means in ref!? Is that something specific in TypeScript, or a new standard JavaScript notation?

like image 558
dbrrt Avatar asked Dec 07 '17 22:12

dbrrt


People also ask

What does an exclamation mark after a variable mean in JavaScript?

The exclamation mark (non-null assertion) operator removes null and undefined from the type of an expression. It is used when we we know that a variable that TypeScript thinks could be null or undefined actually isn't. index.ts. Copied!

What is the double exclamation mark in JavaScript?

operator twice. It converts a nonboolean to an inverted boolean (for instance, ! 5 would be false, since 5 is a non-false value in JS), then boolean-inverts that so you get the original value as a boolean (so !! 5 would be true).

What does an exclamation mark before a variable mean?

! is a logic reversal operator, if something was true it will change it to false, if something is false, it will change to true. example, we know that empty string or 0 in boolean is false.

What is '!' In TypeScript?

What is the TypeScript exclamation mark? The non-null assertion operator tells the TypeScript compiler that a value typed as optional cannot be null or undefined . For example, if we define a variable as possibly a string or undefined, the !


1 Answers

In TypeScript, a postfix ! removes null and undefined from the type of an expression.

This is useful when you know, for reasons outside TypeScript's inference ability, that a variable that "could" be null or undefined actually isn't.

like image 123
Ryan Cavanaugh Avatar answered Sep 28 '22 09:09

Ryan Cavanaugh