Checking if a variable is either true or false [duplicate] - javascript

This question already has answers here:
How to check if type is Boolean
(18 answers)
Closed 3 years ago.
In JavaScript I have a variable initialized with undefined. Now I'd like to check if it is either true or false.
Is there a statement more elegant than the following?
if (isValid === true || isValid === false) {
// do something
}

How about checking if it is not undefined?
If it is neither true, nor false it has to be undefined.
So if it is not undefined, it is set and therefore either true or false.
if (isValid !== undefined) {
// do something
}

Related

How to simplify simple statement in typescript or js [duplicate]

This question already has answers here:
Understanding JavaScript Truthy and Falsy
(9 answers)
Closed 5 months ago.
I would like to simplify this expression but don't know how. There has to be a way to have the same expression without the three conditions.
if ( request.Document != null &&
request.Document != undefined &&
request.Document != "" )
Just for context I am talking something similar to c#'s !string.isNullOrEmpty()
if (request.Document) {
...
}
as null, undefined, '' will all return false.
(but [], {} or ' ' will return true)
Check the equality table for more info about other values.

Why isNaN(true) ( or (isNaN(false) ) is returning false? [duplicate]

This question already has answers here:
Why is isNaN(null) == false in JS?
(9 answers)
Closed 3 years ago.
Just a simple question...
Why this:
isNaN(true)
or this:
isNaN(false)
return false ?
I can understand that false is evaluate to 0 and true to 1 or something...
But is it not the purpose of isNaN(...) to return false only if a variable or a value is "exactly" a number ?
As you think, they return false, because isNaN first coerces its argument into a Number, and then it returns whether the argument is NaN.
Number(true) is 1 and Number(false) is 0, so neither of them is NaN.
To perform a stricter check, and avoid implicit coercion, call Number.isNaN instead:
Both Number.isNaN(true) and Number.isNaN(false) returns true.

If [] is falsy, how come ![] is also falsy [duplicate]

This question already has answers here:
Empty arrays seem to equal true and false at the same time
(10 answers)
Closed 3 years ago.
We know that in JS [] == false returns true. But today I was watching a video presentation by Kyle Simpson and I saw this:
[] == ![] // true
How come ![] is also falsy. As per my logic, it should be true.
== allows coercion. So does ! operator. So [] == ![] becomes "" == false which is true. Further ![] is true because [] is an object which is a true value. And negation of that yields false.
In Javascript, not operator (!) check for not condition like this !(true), here first it checks the inner part of (), If it returns true then the actual output becomes false.
(true) => !(true) =>false
(false)=>!(false)=>true`
Get back to original question
[] return true where as ![] return false when you compare strictly with === its check for type and value both.
console.log([]); // true
console.log(![]); // false
console.log([] == ![]) // true
console.log([] ===![]) // false
Note if you want boolean value all the time then you can use !!.
console.log(!![]); // true
console.log(!!(1 === 1)); // true

How is the boolean statement evaluated in Javascript [duplicate]

This question already has answers here:
All falsey values in JavaScript
(4 answers)
Closed 3 years ago.
So i just found out [0] == true is false, and [0] === true is also false,
so i assumed [0] should be evaluated to false. But when I am writing
if([0]) {
console.log("hi");
}
prints "hi"
That happens because Boolean([0]) returns true. But i think it should have returned false.
What am I thinking wrong here?
JS engine interpretation is that it is a non empty array. Hence, a truthy value in If condition.

Filter Array if object have key false [duplicate]

This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed 5 years ago.
Hi there I'm having small problem, I'm filtering my json on objects but actually as You will guess its does not work could you tell me what I am doing wrong ?
global.selection.Data.Options.filter(function (optionItem) {
return optionItem.hidden === false })
Json :
options : [0 : {name: "some name", hidden:false}]
You are doing a type-specific comparison with === against string 'false', but you actually have a primitive boolean false in your array.
Match against a primitive boolean instead (hidden === false).
Try doing either optionItem.hidden == false or optionItem.hidden === false. As 'false' is a non-empty string hence it is truthy in javascript.

Categories

Resources