Why is `Object() === new Object()` equal to `false`? [duplicate] - javascript

This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Closed 4 years ago.
Why it returns false?
let a = new Object()
let b = Object()
console.log(a) // {}
console.log(b) // {}
console.log(a===b) // false
I checked a proto of a and b too and it is the same.
So what is the difference?j

Instance of objects are not the same even:
let a = new Object();
let b = new Object();
console.log(a===b) // false

Related

overriding valueOf and toString not working as expected [duplicate]

This question already has answers here:
Is it possible to implement equal operator in ES6?
(1 answer)
How to overload operator equality for JavaScript objects
(3 answers)
Closed 2 months ago.
I am trying to override valueOf and toString
This is becouse I want to add some properties to the premitive values
So I created this below
class Box {
#value;
constructor(value) {
this.#value = value;
}
valueOf() {
return this.#value;
}
toString(){
return this.#value;
}
someMethod(){
return "something else"
}
}
const t1 =21;
const t2 = 21
const p = new Box(45);
const s = new Box(45);
console.log(p === s, t1 === t2)
notice that t1 and t2 comparesion works great but p and s are not working as expected
Why is that and is there a way to do the thing I want?

JavaScript Object Property Instantiation [duplicate]

This question already has answers here:
Javascript object initialization and evaluation order [duplicate]
(2 answers)
Are Javascript Object Properties assigned in order?
(5 answers)
Closed 2 years ago.
If you have an object where the properties are created by calling a function or a constructor, is the order of execution of these guaranteed?
Example:
const testObject = {
foo: new Date().valueOf(),
bar: new Date().valueOf()
};
console.log(testObject.foo > testObject.bar);
Is it ever possible that foo will be greater than bar?
You can try it for yourself. In the latest firefox and chrome it appears to be evaluated as written:
let i = 0;
const counter = () => i++;
const testObj = {
a: counter(),
b: counter(),
c: counter(),
d: counter(),
e: counter()
}
console.log(testObj)

Check object key has empty value [duplicate]

This question already has answers here:
How do I test for an empty JavaScript object?
(48 answers)
Closed 4 years ago.
I have object & its key empty value;
like var obj = {newData : {}};
I want to put if condition that return true or false. how can I check that obj.newData has empty object or not?
const obj = {newData : {}};
const obj2 = {newData : {x: 'x'}};
const isEmpty = testObj => Object.keys(testObj).length === 0;
console.log(isEmpty(obj.newData));
console.log(isEmpty(obj2.newData));

concatenating two object keys in JSON [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
console.log(a) ; // console window result= 1
console.log(b);// console window result= 2
var c = {a : b};// any recommendations here?
var d = JSON.stringify(c);
d = encodeURIComponent(d);
I need final result of d = {1:2};
You can use computed property
var c = {[a] : b};

JavaScript object and datatype [duplicate]

This question already has answers here:
How to determine equality for two JavaScript objects?
(82 answers)
Closed 8 years ago.
I am confused why the following does not evaluate to true.
How do I compare whether two objects are the same?
var x = new Object();
var y = {};
x == y // false
function Person(name) {
this.name = name;
}
var p1 = new Person("Chris");
var p2 = new Person("Chris");
p1 == p2 // false
Without going into how a JS engine works you can understand it by just thinking of it like objects are in the real world. If x is a ball and y is a chair, they aren't equal just because they are both objects. And if you know two people named Chris they aren't the same person they just have the same name.

Categories