Object Comparison with same value [duplicate] - javascript

This question already has answers here:
Object comparison in JavaScript [duplicate]
(10 answers)
Closed 7 years ago.
I am checking obeject comparison, below two instance gives 'false' and anyone can explain why?
var a = {x:7, y:9};
var b = {x:7, y:9};
console.log(a==b); //false
console.log(a===b); //false

a is not a primitive value. a is just refers to an object and b refers to another object. Both are holding reference of different object.
Both can not be same

Related

Programmatically Determining when a Variable is a Set [duplicate]

This question already has answers here:
How to check object is a Set? [duplicate]
(3 answers)
How to reliably check an object is an EcmaScript 6 Map/Set?
(5 answers)
Closed 3 years ago.
Is it possible to determine whether a variable is a Set of not in NodeJS?
That is, this code
let thing1 = new Set([1,2,3])
let thing2 = [1,2,3]
console.log(typeof thing1)
console.log(typeof thing2)
reports that both variables contain an object
object
object
Is there a built-in way to determine if one variable contains a set or not? If not, is there a common heuristic used to determine if a variable is a set or not?
Have you tried instanceof?
let thing1 = new Set([1,2,3])
let thing2 = [1,2,3]
console.log(thing1 instanceof Set)
console.log(thing2 instanceof Set)

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

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).

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 ?

What is the purpose of || in a JavaScript variable? [duplicate]

This question already has answers here:
What does the construct x = x || y mean?
(12 answers)
How does javascript logical assignment work?
(6 answers)
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 4 years ago.
I just saw a line of code like this one below and I was intrigued by the use of ||
const myCars = ['BMW','Audi','VW']
const foo = myCars.length || [];
Does this mean if myCars.length was to ever be undefined that foo would equal an empty array instead of undefined?
Yes, how it should be read is:
if 'myCars' doesn't have a length (e.g. no values), the constant foo should be set to [].
Note: https://www.w3schools.com/jsref/jsref_length_array.asp, specifically the return value of the .length: "A Number, representing the number of elements in the array object".
this is Short-circuit evaluation in Javascript. its Unique in JS to USE || operator because other languages use this operator in conditional statements only. please read this
https://en.wikipedia.org/wiki/Short-circuit_evaluation

Finding property in Object [duplicate]

This question already has answers here:
How do I check if an object has a specific property in JavaScript?
(31 answers)
Closed 6 years ago.
Imagine this scenario:
var myObject = {
"1030":{},
"1059":{}
}
I want to check if 1030 is in that object.
How would I do this?
Try hasOwnProperty
if(myObject.hasOwnProperty("1030")) {
// Do code
}
It's a bit safer than checking if(myObject["1030"]). This will return false, if the value is falsey (false, undefined, null), which may be desirable, but also does not strictly mean it does not exist.

Categories