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

This question already has answers here:
In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?
(5 answers)
Closed 5 years ago.
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?

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.

Related

What does ! mean in the context of react refs? [duplicate]

This question already has answers here:
In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?
(5 answers)
Closed 3 years ago.
I was looking through the react-data-grid source code and noticed this line: https://github.com/adazzle/react-data-grid/blob/34a2e51931bba2ac8a2f738cd059945d66516b48/packages/react-data-grid/src/HeaderCell.tsx#L107
return x - this.cell.current!.getBoundingClientRect().left;
I can't seem to figure out what the ! is doing.
That means that you are sure that variable value this.cell.current is not undefined or null

why 2.valueOf() is not valid but (2).valueOf() is? [duplicate]

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 4 years ago.
I guess that javascript will parse (2).valueOf() to new Number(2).valueOf() but why it doesn't for the first one ?
According to the operator precedence, the grouping operator shall have a higher priority than the member access https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
So why (2) is not be evaluated first and yield 2 instead of be parsed to new Number(2) ?
Because in 2.valueOf the . is considered to be as a part of 2 instead of being understood as method accessing.
That is why 2..valueOf() works.
console.log(2..valueOf());

what does `_` mean in javascript? [duplicate]

This question already has answers here:
Javascript: what does function(_) mean
(3 answers)
Closed 5 years ago.
I came across the following code:
Array.apply(null, Array(5)).map(function (_, i) {return i;});
What does _ mean in the above code? I tried replacing _ with null but it is saying syntax error. However undefined would work. I am new to Javascript. So any comments would appreciated! Thanks!
It is just a regular variable. In some languages, like F#, it means that this argument is not intended to be used. For example, see F#'s underscore: why not just create a variable name?

Javascript square brackets around method name [duplicate]

This question already has answers here:
What do square brackets around a property name in an object literal mean?
(2 answers)
Closed 5 years ago.
In the RxJs doc I found following code snippet:
[rxSubscriberSymbol]() {
return new SubjectSubscriber(this);
}
Its part of the Subject source code and is the first method right after the constructor.
So what do square brackets mean in this context?
those are symbols, which is very similar to defining properties but gives different accessibility and testability functionality and they are completely unique,
you can read a lot more about metaprogramming here,
Metaprogramming in ES6: Symbols and why they're awesome

what does 'in' mean in javascript if statement [duplicate]

This question already has answers here:
Javascript if in x [duplicate]
(3 answers)
Closed 6 years ago.
Hi i'm following a tutorial learning to use javascript.
function move(keyclick) {
if(40 in keyclick) {}
playerMove.x++;
render(); }
What does the 'in' word mean? I understand what the function is doing, but why not just use ==
?
Thanks
The in operator is true if the string on the LHS is the name of a property that exists on the object on the RHS.
== tests if a value matches another value, which is entirely different.
The in operator returns true if the specified property is in the specified object (cited from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in).

Categories