Javascript - Why use a double negation [duplicate] - javascript

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;

Related

Meaning of exclamation before dot [duplicate]

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.

In JavaScript what is this method called where you might define a variable or property as such variable_name$ref [duplicate]

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?

Exclamation mark before an object? [duplicate]

This question already has answers here:
What does "!" operator mean in javascript when it is used with a non-boolean variable?
(5 answers)
What is an exclamation point in JavaScript?
(2 answers)
Closed 5 years ago.
Coming from java, learning angular/typescript: The ! negates booleans or boolean expressions. This is, according to my research, the same in javascript/typescript. But what does the following method do?
isSelected(product: Product): boolean {
if (!product || !this.currentProduct) {
return false;
}
return product.sku === this.currentProduct.sku;
}
What is !product? Product is coming in as a method parameter and then it is asked, if product is not product, or wtf should that mean? :D

Can I generate new array with another variable in javascript? [duplicate]

This question already has answers here:
What do square brackets around an expression mean, e.g. `var x = a + [b]`?
(7 answers)
Closed 7 years ago.
function construct(head, tail) {
return cat([head], _.toArray(tail));
}
I'd like to know the role of '[' and ']' above.
Is it an operator?
Is it the array initialize literal?
This question is answered here and on the previous QnA(Use of [square brackets] around JavaScript variables).
I felt this weird because I am not used to javascript.
But this is not a matter any more.
I thought wrong and this is explained. Thanks!
It is initializing a 1 element array to pass to cat here. Brackets can be used to access elements on objects and arrays, and to represent an array literal like you have here.

Why would I use !! in JavaScript? [duplicate]

This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 8 years ago.
While reading lodash source code, I saw:
this.__chain__ = !!chainAll;
Why would one use !! on the chainAll parameter?
I assume this is a safer way to detect falsy values or dealing w/ different JavaScript versions, but would like to know the scenario it protects.
The !! construct is a simple way of turning any JavaScript expression into
its Boolean equivalent. For exmaple: !!"something" === true, while !!0 === false
DEMO
JS use dynamic type for variable. Thanks to this trick you convert the variable into a boolean if it was not a boolean, an it doesn't modify the value if it was a boolean

Categories

Resources