es6 meaning of double exclamation marks [duplicate] - javascript

This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 4 years ago.
in some es6 code I see following:
let layer_combined = layers.map(getLayer).filter(l => !!l);
what is the meaning of !! in the filter() return?

It's a way of casting to a boolean with double negation.
A singular ! negation produces a true/false value, but it's the opposite of what you want. Double negation produces a true/false value which matches the original intent.
Try it:
!!0 // false
!!1 // true
!!"test" // true
!!null // false

Related

Why do I need an exclamation point at the beginning of a condition? [duplicate]

This question already has answers here:
return !1 in javascript
(3 answers)
Closed 10 months ago.
I'm using ParseHub to get links in a conditional manner. I can only extract the URL using a condition in this format, and I'm wondering why I need the exclamation point at the front?
!toLowerCase($e.text).includes("conditional")
The exclamation point negates the expression. You're saying with this line of code that "if the text does not include the string 'conditional', do something..."
An easy way to demonstrate this is with this line of code:
console.log(!true);
We are flipping the condition. So true is obviously true, but !true is equal to false, since it's been flipped.
The ! is called bang or the not operator, and you can read about it here
const string = 'hello world'
// does it include 'hello'?
console.log(string.includes('hello')) // true! it does include 'hello'
// does it NOT include 'hello'?
console.log(!string.includes('hello')) // false. it does include 'hello'
I believe the exclamation point is more of a javascript thing here. Looking at your code snippet it means it negates the .includes result. If it's true, it will be reversed to false and vice versa.

Rules for Converting string and number to Boolean in JavaScript [duplicate]

This question already has answers here:
Equality of truthy and falsy values (JavaScript)
(3 answers)
All falsey values in JavaScript
(4 answers)
Understanding JavaScript Truthy and Falsy
(9 answers)
Closed 5 years ago.
I read this in Eloquent JavaScript book:
The rules for converting strings and numbers to Boolean values state
that 0, NaN, and the empty string ("") count as false, while all the
other values count as true.
Because of this, expressions like 0 == false and "" == false are true.
And also following these rules this expression should evaluate to true:
console.log("A" == true)
But it evaluates to false.
Why?

Why this Javascript RegExp is returning true? [duplicate]

This question already has answers here:
Is it a bug in Ecmascript - /\S/.test(null) returns true?
(2 answers)
Closed 6 years ago.
I need to validate alpha characters (in a request, node.js) and what i used was:
/[a-zA-Z]+/.test(input)
BUT when a value null was passed, then it was validated OK (regexp returns TRUE)
example:
/[a-zA-Z]+/.test(null) // <--- this returns true :(
Somebody can explain me this? i need to avoid nulls, thanks!
The test() method executes a search for a match between a regular
expression and a specified string. Returns true or false
RegExp.prototype.test() converts parameters passed to a String
For example /\d/.test(0) would also return true, as expected, where 0 is a Number passed as parameter.
Because .test() coerces the parameter to a string, you are effectively testing against 'null' - which evaluates to true.

What's !! meaning when used in the of statement? [duplicate]

This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 7 years ago.
Such as if(!!you) , I thought we can get rid of the !! , and it's the same. Cause JavaScript will change it to Boolean automatically?
!! cast the variable to a Boolean. Similar to how you do +foo to cast it to a number.

What's the difference between double exclamation operator and Boolean() in JavaScript? [duplicate]

This question already has answers here:
Is it 100% correct to replace !!someVar with Boolean(someVar)?
(2 answers)
Closed 6 years ago.
I know that !!variable will convert variable into a boolean value and the function Boolean(), according to the ecma262 spec, will also perform a type conversion by calling ToBoolean(value).
My question is: what's the difference? Is !! better in performance than Boolean() ?
They are the same, as the ! operator will call ToBoolean() internally on its operand, and then flip that returned value, while Boolean() will call ToBoolean() internally on its argument.

Categories