Boolean Logic Regarding empty arrays. [duplicate] - javascript

This question already has answers here:
How to determine equality for two JavaScript objects?
(82 answers)
Closed 6 years ago.
quick question:
why does this return false? Just curious.
var myArray = [];
var myArray1 = new Array();
console.log(myArray === myArray1)

Two distinct objects are never === to one another (nor are they ==, for that matter). Object equality means that the two objects are really just one object; that is, that both sides of the === operator are references to the exact same object.
So, this will give you true:
var a = [], b = a;
console.log(a === b);

Every time you do
new Array()
or
= []
A new instance of the Array constructor is assigned to the variable. So when you do a equality check they aint same. Just as same as when two instance of a class aint same.

Related

How can you determine whether a JavaScript object is a list? [duplicate]

This question already has answers here:
How do I check if a variable is an array in JavaScript?
(24 answers)
Closed 1 year ago.
typeof foo will just evaluate to 'object' whether foo is a list [] or a dictionary {} or any kind of object. How can one tell the difference?
In Javascript, arrays are objects, as is the value null. You simply use the Array.isArray method to see if anything is one.
const obj = {}
const arr = []
Array.isArray(obj) // false
Array.isArray(arr) // true

Why is [] !== [] in JavaScript? [duplicate]

This question already has answers here:
Why isn't [1,2,3] equal to itself in Javascript? [duplicate]
(6 answers)
Closed 4 years ago.
Why is [] !== [] in JavaScript?
I read through https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness but I could not find anything that explains this.
Edit:
I don't think this question or this question is an exact duplicate of mine. It asks about the == operator which just behaves crazy. The answer is an answer to my question but it's not the same question.
That does a reference check on the two array literals to see if they are the same instance. The fact that you have two literals means that you are constructing two separate arrays, therefore the reference check returns false. This would return true:
var a = []
var b = a
//b === a
This is because we have two references to the same array.
[] creates a new (and empty) array each time you write it. You are comparing two arrays, regardless of their content, their pointer (or reference) are being compared.
var array = [];
var anotherArray = array; // these two will point to the same array, so they are equal
array === anotherArray; // true
array === []; // false
array.push('something');
anotherArray.length; // 1
Because [] is an object, and a comparison of objects only returns true when both sides of the comparison point to the exact same object. You have created two separate objects, so they aren't equal.
var x = []
var y = x
var z = []
x == x // true
x == y // true
x == z // false

how to simplify this statement using indexOf? [duplicate]

This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
Closed 7 years ago.
How can I simplfy the following text inside the if statement in Javascript using "indexof"?
if (a === 1 || a === 2 || a === 3) {
return "correct";
};
I am guessing an array needs to be made for 1,2, and 3, but am unsure of how to us instanceof after that
*edited to say indexOf instead of instanceOf
The instanceof operator tests whether an object has in its prototype chain the prototype property of a constructor.
In your case, instanceof wont help you. You can use indexOf() with array as follow.
var arr = [1, 2, 3];
// Check if a is present in the arr array
if (arr.indexOf(a) > -1) {
return "correct";
}

Javascript Array Instantiation [duplicate]

This question already has answers here:
What’s the difference between "Array()" and "[]" while declaring a JavaScript array?
(19 answers)
Closed 8 years ago.
What is the correct way on instantiate empty array in javascript?
var array=new Array();
var array=new Array;
var array=[];
All of them look same. Is there any difference?
var array=[];
works best.
Ref - http://jsperf.com/array-instantiation
There is one more way:
var array = Array() ;
They all will produce the same result, an empty object Array. I think the best way is using the array literal shortcut [] as it is what use less characters, it is what most people do.
Note that the same does not apply in the case of Boolean, Number and String, as those constructors will return primitives of type 'object' (object Boolean, object Number and object String respectively) while their literals will return primitives of types 'boolean', 'number' and 'string' respectively (and will behave differently in some situations), but array primitive type it is 'object' in any case.

check object Equal object javascript [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you determine equality for two JavaScript objects?
I want to check if two objects not different :
var v1 = {id:"llll", dd="kkkk"};
var v2 = {id:"llll", dd="kkkk"};
if (v1 == v2)
{
alert("lll");
}
not work why ????
Because objects are compared by reference:
Functions
Objects (Host objects, native objects/constructors and instances)
A common instance is {}, which is similar to new Object.
The following object types are compared by value, not by reference:
Strings
Numbers
Booleans
null and undefined
Additionally, there's one object which is never equal to itself, not even by reference:
var test = NaN;
alert ( test == NaN ); // false
alert ( test == test ); // false (!)
To check whether two objects are equal, you have to define equality:
"Two objects are equal if they contain the same property names and values"
Which implies that object A has to have the same number of properties as object B, and that every property in A has also to be a property of B.
Try using "===" instead of "==" to compare the objects.

Categories

Resources