What's the difference between a == null and a === null? [duplicate] - javascript

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 9 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Can someone help me by explaining the difference. From what I understand the === does an exact match but what does this mean when comparing with null ?

What does this mean when comparing with null?
It means exactly what you already said: It checks whether the value is exactly null.
a === null is true if the value of a is null.
See The Strict Equality Comparison Algorithm in the specification:
1. If Type(x) is different from Type(y), return false.
2. If Type(x) is Undefined, return true.
3. If Type(x) is Null, return true.
So, only if Type(a) is Null, the comparison returns true.
Important: Don't confuse the internal Type function with the typeof operator. typeof null would actually return the string "object", which is more confusing than helping.
a == null is true if the value of a is null or undefined.
See The Abstract Equality Comparison Algorithm in the specification:
2. If x is null and y is undefined, return true.
3. If x is undefined and y is null, return true.

=== means it checks both the value and the type of the variables. For example pulled from the w3c page, given x = 5, x is an int, so x==="5" is false because it is comparing and int to string and x ===5 is true because it is both an int and the right value.

=== is strict operator it not only compare value, but also type of variables so
string===string
int===int
== only compare values.

Using the triple equals, the values must be equal in type as well.But not in ==.
i.e
1==true // this return true
1===true // but return false
a==null // will true if a is null or undefined

1==true will be true
but 1===true will be false
eg. === compares on data type level while using == JavaScript will typecast by it self

Related

What does ?. and ?? operator in javascript do? [duplicate]

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
Null-safe property access (and conditional assignment) in ES6/2015
(11 answers)
Optional Chaining in JavaScript [duplicate]
(8 answers)
Is there a "null coalescing" operator in JavaScript?
(19 answers)
Closed 3 years ago.
I recently noticed usage like this in a java script code base what does it do. I was unable to find any relevant documentation regarding that. Though intuitively operators seem to checking whether property is present. Is there any official documentation regarding this.
Ex:
args?.propertyName !== 'someValue'
const value = props.someProp ?? props.defaultProp;
They are for optionals:
val ?? other is called nullish coalescing operator and is equivalent to val == null ? other : val
and optionalThing?.property is refered as optional chaining and is the same as optionalThing == null ? null : optionalThing.property
This optional chaining expressions result in shorter and simpler expressions when accessing chained properties when the possibility exists that a reference may be missing ( allows you to do things like optionalThing?.optionalProperty?.anotherOptionalProperty?.property ).
The ?. is called the optional chaining operator (TC39 Stage 4), it is used when you are not sure whether a nested property exists or not. If you try to use the . operator to access a property which is undefined you get a TypeError.
For example:
const obj = {foo: {} };
//This is safe, results in undefined
console.log(obj?.foo?.bar?.baz);
//This results in Uncaught TypeError: Cannot read property 'baz' of undefined
console.log(obj.foo.bar.baz);
Where as the ?? is called the null coalescing operator (TC39 Stage 3). When you are using falsy values like an empty string "" or a 0 with a || operator the operand on the right hand side of the || is returned as the falsy value is ignored.
The ?? comes handy when you don't want that and actually you want to consider the falsy values. If the value on the left is a null or undefined only then the value on the right of the ?? is taken:
For example:
const empString = "";
const defaultValue = empString || "A default value";
//Outputs A default value as "" empty string is falsy
console.log(defaultValue);
const actualValue = empString ?? "A default value";
//Does not print the default value as the empString is neither null or undefined
console.log(actualValue);
Same for other falsy values like 0, false, in disagreement with the || operator which will output the 'default string:
console.log(false ?? 'default') //false
console.log(0 ?? 'default') // 0
Only for undefined and null this will output the default value provided in agreement with the || operator:
console.log(undefined ?? 'default') //default
console.log(null ?? 'default') //default

Javascript: If([]) unexpected result [duplicate]

This question already has answers here:
Why if([]) is validated while [] == false in javascript?
(3 answers)
Closed 6 years ago.
I think in every language I know
if(a)
is the same thing as
if(a == true)
Turns out in JavaScript it isn't true, because:
if([])
Seems to act as if the condition is fulfilled, but:
if([] == true)
Does the opposite thing.
I can't really find any possible explanation, especially that this problem doesn't occur with empty string for example (which is == true, but isn't === true, same as empty array). Is this a bug in JavaScript or what?
In JavaScript, there is a concept of truthy and falsey values. if statements test the truthiness or falsiness of a given value rather than strict equality to true or false.
true is obviously truthy. false is obviously falsey. The rest can be a little tricky. MDN has possibly the clearest documentation about which values evaluate to falsey: https://developer.mozilla.org/en-US/docs/Glossary/Truthy
In this case [] is a truthy value so the condition passes and the code is executed.

Object is not null and not undefined. What is better - double inversion `!!` or strict equillity `!==` in JavaScript [duplicate]

This question already has answers here:
JavaScript checking for null vs. undefined and difference between == and ===
(8 answers)
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 7 years ago.
Very often we check our objects if they are not null and not undefined. I always use condition if (obj !== null && obj !== undefined). Few days ago my colleague shown me the syntax of double inversion !! and now I can use condition if (!!obj). This syntax is less.
I'm not a person who are only learning js, but I have a little interest.
So is there any difference between these two ways of object validation? Performance difference? Semantic difference? Any difference?
There isn’t any particularly good reason to ever use if (!!obj), as it’s equivalent to if (obj). !!obj and obj !== null && obj !== undefined do different things, though, and you should use whichever’s most appropriate.
obj !== null && obj !== undefined (surprise, surprise) results in false for null and undefined.
!!obj results in false for anything falsy, including null, undefined, '', 0, NaN, and false.
!!foo is used to force coerce a value into its Boolean value. This is, in my experience, often used in APIs that want to return a Boolean value for a value that contains sensitive data. For example, to return whether or not a password is entered you might say return !!password rather than return password.
Continuing on that, if (!!obj) is not the same as if (obj !== null && obj !== undefined) for many values you can think of! Such as false.

JavaScript: what is the meaning of "!!"? [duplicate]

This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Can someone explain this 'double negative' trick? [duplicate]
(9 answers)
Closed 8 years ago.
I just ran into this
var strict = !! argStrict;
I can't help but wonder, what is the meaning of !!? Is it a double negative? Seems pretty redundant, not likely that the guys from php.js would use that in such case?
source: http://phpjs.org/functions/in_array/
It forces the type to become a true boolean value rather than a "truthy" value.
Examples:
var a = (1 === true) // comes out as false because 1 and true are different types and not exactly the same
var b = ((!!1) === true) // comes out as true because the 1 is first converted to a boolean value by means of negation (becomes false) and then negated a second time (becomes true)
It means basically "convert to boolean".
It's negating it twice, so it argStrict is "falsy", then !argStrict is true, and !!argStrict is false.
It returns a boolean value. false for undefined, null, 0, '' and true any truthy value.
This is an example of slacker parsing.
If you have a variable, for example a string, and you want to convert it into a boolean, you could do this:
if (myString) {
result = true;
}
This says, if myString is NOT undefined, null, empty, the number 0, the boolean false then set my string to the boolean value to true...
But it is faster, and way cooler to double bang it:
result = !! myString;
Other examples include....
//Convert to number
result = +myString;
//Bang Bang Wiggle - coerces an indexOf check into a boolean
result !!~myString.indexOf('e');

what does "===" imply in Javascript/Jquery? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?
The 3 different equals
I'm trying to understand what is happening here:
data.toPage = $('div#someID');
if ( typeof data.toPage === "string" ) {
// sth
console.log("hello");
}
So I'm checking for a string am I not? I'm curious because my console "helloes".
Thanks for some input!
==
This is the equal operator and returns a boolean true if both the operands are equal. JavaScript will attempt to convert different data types to the same type in order to make the comparison. Assuming 'a' to be 2 and 'b' to be 4, the following examples will return a value of true:
a == 2
a == "2"
2 == '2'
===
This is the strict equal operator and only returns a Boolean true if both the operands are equal and of the same type. These next examples return true:
a === 2
b === 4
The triple equals sign === compares both value and type, whereas the double == only compares value
for example "1" and 1 have the same value (so to speak) but are of different types. Therefore the following will occur:
"1" == 1 //true
"1" === 1 //false
This is a great read for some useful javascript knowledge, which includes the triple equals amongst other good-to-know stuff
The === comparison operator means that the two values won't have their types modified before the comparison is made, so they need to be of the same type as well as representing the same value for it to return true.
'1' == 1 // true
'1' === 1 // false

Categories