Arrays in JavaScript behaves like an Object Last Updated : 31 Jan, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how you check that javascript arrays behave like objects and why it happens. We will be showing the key and values in the array whether you define the key or not in an array of JavaScript. Reason for Arrays behaving like an object: In JavaScript, there is no real Array instead of that it behaves in an array because the Array Class has been extended from the Object Class due to which properties of an object can be used in arrays as well.Every Class in JavaScript except primitive data type has been extended from the object class due to which properties of an object can be used in other data types as well. Let's see the implementation for the above problem with an explanation as given below: Example 1: If we will not be defining a key in the array then by default the key is set to the index of the value. JavaScript // Initialized the array let arr = []; // push() is used to add the element in last arr.push(5); arr.push(10); arr.push("Prince"); arr.push("Aditya"); // Accessing the keys of array // using the object property let keys = Object.keys(arr).join(" "); // Accessing the values of array // using the object property let values = Object.values(arr).join(" "); console.log("The keys of array is: " + keys); console.log("The values of array is: " + values); Output: The keys of array is: 0 1 2 3 The values of array is: 5 10 Prince Aditya Example 2: In this, we will be defining the key manually and accessing the value as per the key by using the object property. JavaScript // Initialized the array let arr = []; arr["GeeksforGeeks"] = "GFG"; arr["Aditya"] = "Sharma"; arr["Prince"] = "kumar"; // Accessing the value from the array // using the object properties let value1 = arr["GeeksforGeeks"]; let value2 = arr["Aditya"]; let value3 = arr["Prince"]; console.log("Accessing the value of GeeksforGeeks: " + value1); console.log("Accessing the value of Aditya: " + value2); console.log("Accessing the value of Prince: " + value3); Output: Accessing the value of GeeksforGeeks: GFG Accessing the value of Aditya: Sharma Accessing the value of Prince: kumar Comment More infoAdvertise with us Next Article Arrays in JavaScript behaves like an Object S sdeadityasharma Follow Improve Article Tags : JavaScript Web Technologies DSA Technical Scripter 2022 javascript-array javascript-object JavaScript-Questions +3 More Similar Reads Difference Between JavaScript Arrays and Objects Below are the main differences between a JavaScript Array and Object.FeatureJavaScript ArraysJavaScript ObjectsIndex TypeNumeric indexes (0, 1, 2, ...)Named keys (strings or symbols)OrderOrdered collectionUnordered collectionUse CaseStoring lists, sequences, ordered dataStoring data with key-value p 1 min read Are JavaScript arrays objects? Yes, JavaScript arrays are actually specialized objects, with indexed keys and special properties. They have a length property and are technically instances of the Array constructor.JavaScriptconst a = [10, 20, 30]; console.log(typeof a);Outputobject You can add non-integer properties to arrays, mak 2 min read JavaScript- Convert an Object to JS Array Objects in JavaScript are the most important data type and form the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data types (Number, String, Boolean, null, undefined, and symbol). Methods to convert the Objects to JavaScript Array:1. Using Obje 3 min read How to Push an Array into Object in JavaScript? To push an array into the Object in JavaScript, we will be using the JavaScript Array push() method. First, ensure that the object contains a property to hold the array data. Then use the push function to add the new array in the object.Understanding the push() MethodThe array push() method adds one 2 min read JavaScript - Convert an Array to an Object These are the following ways to convert an array to an Object:1. Using JavaScript Object.assign() method The first approach is using the Object.assign() method. This method copies the values of all enumerable properties from source objects(one or more) to a target object.JavaScriptlet a = [1, 2, 3, 2 min read Array vs. Object Efficiency in JavaScript In JavaScript, both arrays and objects are used for storing data, but they are suited for different use cases. Understanding their efficiency is key to choosing the right data structure for your application.If you want to know the full comparison between arrays and objects, click here.Use Cases for 2 min read How to Declare an Array in JavaScript? Array in JavaScript are used to store multiple values in a single variable. It can contain any type of data like - numbers, strings, booleans, objects, etc. There are varous ways to declare arrays in JavaScript, but the simplest and common is Array Litral Notations. Using Array Literal NotationThe b 3 min read Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J 3 min read How to Access Array of Objects in JavaScript ? Accessing an array of objects in JavaScript is a common task that involves retrieving and manipulating data stored within each object. This is essential when working with structured data, allowing developers to easily extract, update, or process information from multiple objects within an array.How 4 min read JavaScript- Add an Object to JS Array In JavaScript, arrays are used to store multiple values in a single variable, and objects are collections of properties and values. Sometimes, we may need to add an object to an array to manage data more effectively. These are the following ways to add an object to a JS array: Using JavaScript Array 4 min read Like