0

I have studied that an Array has homogenous elements and a List has heterogenous elements. But, in JavaScript, we call it Array and it takes elements of different data type too. How is that possible? Isn't it contradictory to the definition?

8
  • 2
    What exactly were you studying? Was there a certain book, course, or instructor? Commented Oct 8, 2021 at 22:10
  • 2
    Arrays are list-like objects in javascript. Always read the docs before asking. Commented Oct 8, 2021 at 22:12
  • 1
    Is your question about which kinds of things can go in Arrays? Ie. either all things of the same type, or things of a different type. In JavaScript it's the latter. Addons like TypeScript can greatly aid the type-safety however. Commented Oct 8, 2021 at 22:15
  • @pilchard I already know it's an object. My question was focus on the literal meaning of the terminology. And, I have read the docs but it doesn't always clear all the doubts. Commented Oct 8, 2021 at 22:22
  • @Halcyon Yes, I know the type safety feature of typescript. Thank you for your help anyways. Commented Oct 8, 2021 at 22:25

1 Answer 1

2

Arrays in Javascript are a special kind of object. It's basically an object with numeric keys that are ordered (starting from 0). Also, this object has special methods for dealing with this kind of data structure.

Therefore, an object in Javascript follows the category of dictionary data structure:

In JavaScript, an object is a collection of key-value pairs. This data structure is also called map, dictionary or hash-table in other programming languages.

https://www.freecodecamp.org/news/data-structures-in-javascript-with-examples/#objects-hash-tables-

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.