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
Related
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 ?
This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Closed 6 years ago.
I was going through Map Documentation on MDN. In Examples, under Using Map Object, Object Literal - {} is used as key to store value. But, the value in Map can't be retrieved using Object Literal.
I verified this in Browser Console and found that Object Literal is not equal to itself. Also, the Function Expression - function() {} is not equal to itself.
I couldn't find the reason behind this. If required, I can ask a different question for Function Expression.
Each time you do {}, it creates a new empty object, so when you do {} == {}, you're comparing two different objects. This comparison is done by reference, so it returns false.
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
This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Closed 3 years ago.
I was writing a script, and I had something like
var myObject = {};
if (myObject == {}){
...
}
I was surprised to find that this evaluated to false.
My own findings
Reading up on some of the funny querks javascript does, I found this article, which tells that {} will result in a "truthy" object in an if statement, for example
if ({}){ // true
...
}
Hm, well further ready discuesses String Equality, which for object comparison, says it will return true if they reference the same object. (Even {} === {} returns false)
The only reason I can think that is would return false is that the objects are technically at different memory address, and thus considered different objects.
Because every {} creates a unique object. You can't compare them that way.
This question already has answers here:
Are Javascript arrays primitives? Strings? Objects?
(7 answers)
Closed 8 years ago.
A colleague, by mistake wrote this code:
var parameters = [];
// some lengthy code here
parameters.firstParameter = "first parameter value";
parameters.secondParameter = "second parameter value";
He had declared parameters variable as an array, but somewhere else he had used it as an object, adding parameters to it.
The results of testing the type of this parameter is as follow (in Google Chrome's console):
parameters;
// prints []
typeof parameters;
// prints "object"
parameters instanceof Array;
// prints true
So, is it an object or an array at last? Or does it have a dual nature, both array and object at the same time?
In JavaScript everything is an Object. If you define an Array so it'll be treat as an object in JavaScript. Since it is an object, you can access properties associated to it.