Yogesh Chauhan's Blog

JavaScript arrays: a separate data type or Objects?

in JavaScript on January 4, 2021

Are arrays a separate data type?

No, JavaScript arrays are not a separate data type. In JavaScript, arrays are objects.

Arrays are Objects

The JavaScript Array class is a global object.

Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations.

Neither the length of a JavaScript array nor the types of its elements are fixed.

Arrays, then, are just a kind of object specialized for storing sequences of things.

If you evaluate typeof [], it produces “object”.


var cars = ["Audi", "Toyota", "Tesla"]
console.log(typeof(cars));

// object

You can see them as long, flat octopuses with all their tentacles in a neat row, labeled with numbers.

Arrays cannot use strings as element indexes but must use integers.


var cars = ["Audi", "Toyota", "Tesla"]
console.log(cars[0]);

// Audi

Using an invalid index number returns undefined.


var cars = ["Audi", "Toyota", "Tesla"]
console.log(cars[3]);

// undefined

Array elements are object properties in the same way that toString is a property (or a method).

But, trying to access an element of an array as follows throws a syntax error because the property name is not valid:


var cars = ["Audi", "Toyota", "Tesla"]
console.log(cars.0);

// SyntaxError

📌 JavaScript properties that begin with a digit cannot be referenced with dot notation and must be accessed using bracket notation.

Arrays are special kinds of objects.

Because of this, you can have variables of different types in the same Array.

You can have objects in an Array:


var cars = [
  {firstName:"John", lastName:"Doe", age:28},
  {firstName:"Yogesh", lastName:"Chauhan", age:28}
]
console.log(cars[1]);

// { firstName: 'Yogesh', lastName: 'Chauhan', age: 28 }

You can have functions in an Array:


var array_of_functions = [
  printFullName("Yogesh"),
  printFirstName("Yogesh"),
  ]

console.log(array_of_functions[0]);

function printFullName(name){
  return name + " Chauhan";
}

function printFirstName(name){
  return name;
}

// Yogesh Chauhan

You can have arrays in an Array:


var array_of_arrays = [ 
   ["a", "b"], ["a", 2], [4,100]
  ]

console.log(array_of_arrays[0]);

// [ 'a', 'b' ]

Sources


amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS? #Aug 1 What is Zdog? #Aug 1 How to add before after image effect using pure CSS? #Jul 31 Add animation to your skills bar using CSS #Jul 31 Use SwiperJS to create mobile touch sliders fast
You might also like these
How to find the HCF or GCD and LCM of two given numbers using Swift?SwiftOpen Source Security Tools for Defense SecurityMiscellaneous4 different Ways to Get JavaScript OutputJavaScriptWhat is Prototypal Inheritance in JavaScript?JavaScriptHow to create a full screen loader using CSS and JavaScript?CSSIntroduction to Angular modules Part 2: NgModules and componentsAngular