Strict equality works on variables but not on objects [duplicate] - javascript

This question already has answers here:
How to determine equality for two JavaScript objects?
(82 answers)
Why is `Object() === new Object()` equal to `false`? [duplicate]
(1 answer)
Closed 3 years ago.
I have a variable defined as:
var o = new String("0");
In console when i write:
o === o
it returns true
but when i write:
new String("0") === new String("0")
it returns false
I don't understand why is it working on variable references but not on objects?
I tried it as:
(new String("0")) === (new String("0"))
because the problem may arise due to operator precedence, but it still returns false

new String("0") === new String("0")
Here you are comparing two different Strings having different references. Thats why you are getting false.
o === o
Here, You are actually comparing the same string (reference is same in this case).

Related

Comparing Custom Strings(classes extending String class) using == [duplicate]

This question already has answers here:
Is it possible to implement equal operator in ES6?
(1 answer)
How to make comparison of objects `a == b` to be true? [duplicate]
(2 answers)
Javascript: operator overloading
(9 answers)
Closed 8 months ago.
I was wondering, if there are any ways on how to compare 2 custom string objects which are instances of classes which extend String, using == doesn't work and it always returns false.
I have tried using valueOf but with no luck, the method gets never called if both operands are objects.
See an example below
class MyString extends String {
doSomething(someData) {
return doSomethingWithData(this, someData);
};
valueOf() {
console.log('valueOf called');
return this.toString();
};
};
When doing console.log(new MyString('hi') == 'hi');, I Get
valueOf called
true
But When doing console.log(new MyString('hi') == new MyString('hi'));, I Get false.
So, the valueOf gets never called and the comparison blindly returns false, is there any possible way to fix it?

Javascript comparing object [duplicate]

This question already has answers here:
Object comparison in JavaScript [duplicate]
(10 answers)
Closed 4 years ago.
When you comparing object with another object with same property why it returns false?
For Example
var person={
age:30
}
var person2={
age:40
}
console.log(person==person) or console.log(person===person)
it show's in console false why?
Objects are reference types, which means the equality operators operate on a reference to the object in memory, not to its contents.
In your particular case, you could serialize the object to a string and then check
const compareSerializableObjects = (a, b) =>
JSON.stringify(a) === JSON.stringify(b)
person === person will always return true as you are comparing the same reference, and if you are comparing person === person2 then it is a different refference which is false.
Did you mean person.age === person2.age ?

Why === and == giving false for following? [duplicate]

This question already has answers here:
Why doesn't equality check work with arrays [duplicate]
(6 answers)
Closed 4 years ago.
I know it is very much stupid to ask but can anyone tell me
Why === and == giving false for following.
x=[[1,2]];
console.log(x[0]===[1,2]);
console.log(x[0]==[1,2]);
Here typeof(x[0]) and typeof([1,2]) is also same, then why it is giving false?
Because they are different values in memory.
x=[[1,2]];
console.log(x[0]===[1,2]); // Here you're creating a new array in memory
console.log(x[0]==[1,2]); // Here you're creating a new array in memory
var y = x[0]; //Same value in memory
console.log(x[0]===y);
console.log(x[0]==y);
Equality comparisons and sameness

Why is the equality operator on object returning false? [duplicate]

This question already has answers here:
Object comparison in JavaScript [duplicate]
(10 answers)
Closed 7 years ago.
I'm trying to manipulate the view of a web messaging system and the default view is a blank object, {}. I used an ng-show="data.currentView == {}" in my AngularJS script to check if it is currently the default view, but it never showed. Then I tried this in my Javascript Console in Chrome:
var data = {}
console.log(data == {})
//Logs false
console.log(data === {})
//Logs false
Why is that statement returning false?
You are testing to see if two objects are the same object, not if they are identical objects.
See the specification:
If Type(x) is the same as Type(y), then … Return true if x and y refer to the same object. Otherwise, return false.
You are comparing references with objects, so they are never equal. A good way would be:
Object.keys(data.currentView).length == 0

test if a variable is a primitive rather than an object? [duplicate]

This question already has answers here:
Check if a value is an object in JavaScript
(54 answers)
Closed 7 years ago.
Is it possible to test a variable to see if it is a primitive?
I have seen lots of questions about testing an variable to see if it is an object, but not testing for a primitive.
This question is academic, I don't actually need to perform this test from my own code. I'm just trying to get a deeper understanding of JavaScript.
To test for any primitive:
function isPrimitive(test) {
return test !== Object(test);
}
Example:
isPrimitive(100); // true
isPrimitive(new Number(100)); // false
http://jsfiddle.net/kieranpotts/dy791s96/
Object accepts an argument and returns if it is an object, or returns an object otherwise.
Then, you can use a strict equality comparison, which compares types and values.
If value was an object, Object(value) will be the same object, so value === Object(value). If value wasn't an object, value !== Object(value) because they will have different types.
So you can use
Object(value) !== value

Categories