What is the main difference between array and object in js [duplicate] - javascript

This question already has answers here:
What’s the difference between “{}” and “[]” while declaring a JavaScript array?
(8 answers)
Closed 4 years ago.
The point of confusion is - when we check the type of array or object using typeof get the return value as Object. So what's the main difference between them.

You can check using constructor.name
const myArr = []
const myObj = {}
console.log(`myArr is an ${myArr.constructor.name}`)
console.log(`my Obj is an ${myObj.constructor.name}`)

Related

What does this constant definition mean [duplicate]

This question already has answers here:
What is destructuring assignment and its uses?
(3 answers)
Closed 8 months ago.
What does this assignment mean?
const { data: scoreData } = useMostRecentScore(studentId, loginId)
If useMostRecentScore returns an object, with a data property, it will create a new variable called scoreData and assign the contents of the data property.

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

Is there a difference between {} and new Object() [duplicate]

This question already has answers here:
What is the difference between `new Object()` and object literal notation?
(12 answers)
Closed 1 year ago.
I was asked this in an interview. I couldn't answer. Not sure if it was a trick question.
let a = {}
and
let a = new Object
new Object()
has a default argument value of {}
You can also initialize an Array with new Object
const a = new Object([]);
See here.

Are Javascript objects always unique [duplicate]

This question already has answers here:
How to determine equality for two JavaScript objects?
(82 answers)
How to explain object references in ECMAScript terms?
(1 answer)
Javascript Object Identities
(6 answers)
Closed 1 year ago.
If I do:
const o1 = {};
const o2 = {};
is it guaranteed that o1===o2 is false?

Is it possible to create a ES6 Javascript expression whose value is an object with a dynamic property name? [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
I currently do:
function outer(prop_name) {
const tmp = {};
tmp[prop_name] = 'hello world';
foo(tmp);
}
Is there a way of rewriting this as:
foo(<expression>)
using an expression involving prop_name?
You can write it as
foo({ [prop_name] : 'hello_world'});

Categories

Resources