Is it object, or array? Which one? [duplicate] - javascript

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.

Related

I can't iterate over a javascript array [duplicate]

This question already has answers here:
How can JavaScript arrays have non-numeric keys?
(2 answers)
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 4 months ago.
I am going crazy here, I have an associative array as seen below which is defined after the page finishes loading. However Array.forEach is returning undefined and I have no idea why. The array is most definitely populated during the loop. Can anyone give me any ideas? Also doesn't work with JQuery's $.each
Arrays are usually a mapping of index (integer from 0 to 232 − 2, inclusive) to value. In your case you've treated the array as a dictionary e.g. key (string) to value.
You've probably done something like this:
members = new Array();
members['animerox1213'] = 'Ashima';
JavaScript allows this, after all it is still an object:
typeof members === 'object'
But instead of adding a value to the array, you've actually set a non-numeric property on the object called animerox1213. That is not how an array should be used and we can observe this by checking the size:
members.length === 0;
Consequently, forEach does not do anything as it considers it an empty array.
That said, it is enumerable with for…in as it's still just an object (with enumerable properties):
for (m in members) {
console.log(m, members[m]);
}
Consider using just an object e.g. members = {} or Map. Note especially the section Objects vs. Maps.

Empty array is shown as object [duplicate]

This question already has answers here:
How can I check if an object is an array? [duplicate]
(51 answers)
Closed 1 year ago.
I wrote a utility function for my nextjs project. In there, I got something that I unexpected. I initialised an empty array which be filled with the data later. When I type check that empty array, it shows an object. I don't want an object. I want an array. Why? Could someone told me why does it happen.
strong text
Yes typeof array is an object in javascript
if you want to check one variable is an object or array then you can use
Array.isArray()
console.log(Array.isArray(arr)) // true
console.log(Array.isArray(obj)) // false

logging javascript object properties [duplicate]

This question already has answers here:
console.log() async or sync?
(3 answers)
console.log() shows the changed value of a variable before the value actually changes
(7 answers)
Closed 5 years ago.
Please share your thoughts regarding below outputs.
var a = '';
console.log(a); //this logs nothing as expected.
a = 'triven';
var a = {};
console.log(a); // How javascript is aware of property here that is defined below.
a.name = 'triven';
I think you're looking for console.dir().
console.log() doesn't do what you want because it prints a reference to the object, and by the time you pop it open, it's changed. console.dir prints a directory of the properties in the object at the time you call it.
The JSON idea below is a good one; you could even go on to parse the JSON string and get a browsable object like what .dir() would give you:
console.log(JSON.parse(JSON.stringify(obj)));

Why {} !== {} in Javascript [duplicate]

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.

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