Difference between !a and a=== null - javascript

Is there a difference between !a and a===null in JavaScript?
I am working on a project where it is behaving differently.
I am not talking in terms of Boolean values though as !true will not me same as true===null
if (!this.props.user) is resulting to true,
where as if (this.props.user === null) is resulting to false

!a will return true if a is any Falsy value (e.g, false, undefined, null, 0...)
a === null will only return true if a is null.

like #Spectric answer.
a === null need to have same type to return true.
even the value has falsy value (e.g, false, undefined, 0). it will return false because the value is not same with null

Related

Javascript - is null or true - What would be the most elegant solution?

Since we all know null is evaluated as false, how could I write "is null or true" in Javascript?
I have tried:
function isNullOrTrue(nullableBool){
return !(nullableBool != null && nullableBool != true);
}
Is there a better way to do this?
If the three possible inputs are only null, true and false, you can use
return nullableBool !== false
but explicitly listing the matching values isn't bad for readability either
return v === null || v === true
Using === instead of == isn't actually necessary here, as null != false (whereas Boolean(null) == false), but it might help for clarity if you're not familiar with the loose equality rules.
The elegant solution to is null or true is to use nullish coalescing operator
return nullableBool ?? true
It will return true if nullableBool is either null or undefined. Otherwise, it will return the value of nullableBool. If you only want to return true or false, then you can use double bang:
return !!(nullableBool ?? true)
Now, it will return true if nullableBool is either null, undefined or truthy value. Otherwise, return false.

Uncertainty with the !! operator (double negation)

I saw in a pull request that the double negation operator (!!) is used for the focus attribute of a text field as follows:
focused: !!value || value === 0,
As far as I know, the operator converts everything to a boolean. If it was falsy (for example 0, null, undefined,..), it will be false, otherwise, true.
In my case, i.e. if value = 0, the following comes out:
focused: false || true
The || operator here therefore makes no sense for the value 0 or am I completely confused?
It looks like a check for numbers to get false for '', "", false, NaN, undefined and null. Other bjects, like functions, arrays or simple objects returns true;
const check = value => !!value || value === 0;
console.log(check(0));
console.log(check(1));
console.log(check(''));
console.log(check(""));
console.log(check(false));
console.log(check(NaN));
console.log(check(null));
console.log(check(undefined));
console.log(check({}));

In if statement,undefined equals with false

I'm confused with the code below:
if(undefined){
//code will not be executed
}
and
if(!undefined){
//code will be executed
}
Is that mean the "undefined" equals with false?
Here the question related,but no one point above situation out.
It means that undefined is a falsy value, list of falsy values are:
"" // Empty string
null // null
undefined // undefined, which you get when doing: var a;
false // Boolean false
0 // Number 0
NaN // Not A Number eg: "a" * 2
If you negate a falsy value you will get true:
!"" === true
!null === true
!undefined === true
!0 === true
!NaN === true
And when you nagate a truthy value you will get false:
!"hello" === false
!1 === false
But undefined is not equal false:
undefined === false // false
undefined == false // false
And just for the fun if it:
undefined == null // true
In javascript strict mode, undefined is not false, but javascript try to convert the object or var to a boolean value (this is called in javascript truthy value), that's the reason you got an undefined as false. This happens with null also, for example.
You can force that with this strict no equality:
if(undefined!==false) console.log("Is not false");
Please take a look below checked falsy values:
""==false?
Ans: true
null == false?
Ans: false
undefined == false?
Ans: false
0 == false?
Ans: true
NaN == false?
Ans: false
null == NaN?
Ans: false
We can see that null == false,undefined == false,null == NaN, and NaN == false are not true
That means they are not equal. From the above result, we got 3 falsy values group:
The False group
The Null group and
The NaN group
But a negative falsy value is always true:
!"" === true
!null === true
!undefined === true
!0 === true
!NaN === true
For example:
To check true value of dataTitle variable
if(dataTitle && (dataTitle != null))
{
console.log('hi');
}
The above statement will check the false group as well as the null group
To check false value of dataTitle variable
if(!dataTitle)
{
console.log('hi');
}
//or
if(dataTitle==null || dataTitle===false)
console.log('hi');
In javascript, undefined and null are empty properties declared on the global scope.
Their value doesn't equal false; they both have the initial value of primitive undefined.
undefined == false // false
With that said, both will result in false value upon a programmatic true/false evaluation. In your example, you used a logical NOT operator (MDN):
Returns false if its single operand can be converted to true; otherwise, returns true
The negation operator first evaluated undefined as false, and then negated the value to true. You can illustrate the process roughly like this:
if (!(!!undefined)) {
// code will be executed
}

Why doesn't empty string == null in JavaScript

I understand that both empty string and null are falsy according to the ECMAScript. If both are falsy then why doesn't the following evaluate to true?
var emptyString = '';
if (emptyString == null) {
console.log('emptyString == null');
}
else {
console.log('emptyString does not == null'); // but why?
}
both empty string and null are falsy
Yes, but that doesn't mean all falsy values would be equal to each other. NaN and 0 are both falsy as well, but they're definitely not equal. The reverse doesn't hold either, "0" == 0 but "0" ain't falsy.
The sloppy equivalence of values is defined by the Abstract Equality Algorithm and its type coercions, and null simply isn't == to anything but undefined.
The more commonly used abstract comparison (e.g. ==) converts the operands to the same Type before making the comparison.
Here, null is a falsy value, but null is not == false
The falsy values null and undefined are not equivalent to anything except themselves:
(null == false); // false
(null == null); // true
(undefined == undefined); // true
(undefined == null); // true
since the other operand is null( which is also a type in javascript ), the abstract comparison of empty string(falsy value) and null doesn't give a truthy value.
I think this will help you.
Comparison Operators
and this too
Truthy and Falsy: When All is Not Equal in JavaScript

How can I easily set a variable to false with a default of true?

I usually set object properties like so,
// Boolean
this.listening = config.listening || true;
But config.listening is either true or false, and in this case this.listening will always be true because if config.listening is false it will equal true.
Is there a better way to set these boolean properties without having to do an if statement?
Is there a if isset function in javascript to check it exists rather than what it equals to?
You could use the ternary (conditional) operator like this:
this.listening = config.listening === false ? false : true;
If config.listening is false, this.listening is set to false. If it's any other value, it's set to true.
If you want to check if it's defined, you could use:
this.listening = typeof config.listening !== "undefined"
References:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Conditional_Operator
You need to check to make sure it is not undefined, not that it is a "falsey" value.
this.listening = config.listening!==undefined ? config.listening : true;

Categories