This question already has answers here:
In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?
(5 answers)
Closed 7 months ago.
I've seen somewhere written in the code fixture.componentInstance.dataSource!.data = [];
I want to know the meaning of dataSource!.data
I know about the meaning of question mark(?) before dot(.) like key1?.key2 but an Exclamation(!) before dot(.) !. expression makes me curious.
Many thanks in advance!!
It's called the Non-null Assertion Operator
https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#non-null-assertion-operator-postfix-
It removes null and undefined, allowing you to assert that you know there will be a value. Usually this is a code smell, something that should give you pause to consider other methods so that you can avoid using it. Type narrowing would be preferred. But in some specific instances, this operator can be a useful tool.
Related
This question already has answers here:
In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?
(5 answers)
Two sets of parentheses after function call
(4 answers)
Closed last month.
In Codemirror 6's documentation, and in the code line 41, ...
while (pending.length) pending.pop()!(data.updates)
What does this syntax mean?
Seems to be typescript specific.
What would be the javascript equivalent?
In TypeScript, the ! operator can also be used to assert that a value is non-nullable. This is called a non-null assertion operator, and it is used to tell the TypeScript compiler that a value is definitely not null or undefined. Check the documentation
The pop() method removes the last element from an array and returns that element. See the documentation.
Since the return value is a function, well you can call it.
This question already has answers here:
Rules for unquoted JavaScript Object Literal Keys?
(6 answers)
What characters are valid for JavaScript variable names?
(12 answers)
Closed 1 year ago.
I am seeing it crop up more and more in code I am going through on a new project (can't share due to contractual reasons) where Ill see something like:
{
prop1: value$ref,
$prop2: null
}
I have see ${prop3} before, but never an example without the brackets. Can anyone provide direction as to what the method is, or the operator is or whatever the case?
This question already has answers here:
How to interpret function parameters in software and language documentation?
(4 answers)
Closed 2 years ago.
I'm trying to get a good sense of reading documentation instead of asking so many questions here and there.
The first thing i'm confused of is the symbols.What do the symbols below mean?
example) app.use([path,] callback [, callback...])
reference source:https://expressjs.com/en/5x/api.html#app.use
Question1&2 can be solve by the similar posting.
Another example is like
bisector.left(array, x[, lo[, hi]])
what does x[, mean?
what does **lo[,**mean?
what does ,hi] mean?
reference source: https://devdocs.io/d3~5/d3-array#bisect
It would be very grateful if anyone could help me to under stand these cryptic symbols.
[xxx] means that this parameter is optional. You can use it or omit it.
[callback] is also an optional parameter. In this case it's a function that will be called under some circumstances.
Nested bisector.left(array, x[, lo[, hi]]) means that you need to have some options before you can specify others. For example, you cannot specify lo in this case without specifying hi first.
This question already has answers here:
Why use if (!!err)?
(3 answers)
Closed 8 years ago.
Within an if condidition, when does it make sense to use a double negation? I found this pattern several times:
JS
if(!!children.length) { ...}
It's a common method to turn a normal expression to a boolean. It doesn't really make any sense inside an if statement, since it evaluates the given expression as a boolean anyways, but is useful outside.
For example:
const isEmpty = !!children.length;
This question already has answers here:
Can someone explain this 'double negative' trick? [duplicate]
(9 answers)
Closed 9 years ago.
Is there any difference (in JS) between using the double negative !! and not using it at all?
For example
if (!!variable){... vs. if (variable){...
I know there are times where I've gotten a warning using the 2nd method..
When should each be used? and when will each throw a warning in the console? (for variables, objects, arrays etc.)
Thanks!!
There is a difference for assigning it, but not for using it in a conditional statement. The reason the !! is used is because the first ! will convert your variable to its truthy evaluation and then not it. So "hello" becomes true, is then negated, becomes false, and the second ! will negate the false, resulting in true. This can be desirable when trying to obtain the thruthy value from a variable. However, there is not much gained by doing it in an if statement.
In this particular case, there is no difference. In fact, !!variable is wasteful.
However in more general cases, it casts the variable to a boolean. Personally I've only found this useful when debugging, and to learn what values are truthy and falsy.