Complex types: Objects and Arrays

Complex types: Objects and Arrays

In JavaScript programming, object and array data types are treated as complex data types or non-primitive data types because arrays and objects in JavaScript can store and process data and information in multiple program variable values ​​or more complex structures. Object and array data types are the exact opposite of primitive data types, which store and process single program variable values ​​in JavaScript programs. Both objects and arrays can be used to arrange, store, or manage complex program data in JavaScript programs.

Complex types Objects and Arrays

Object Data Type in JavaScript.

Object data type in JavaScript programs is a unique storage collection of key-value pairs. Where each key or property in the created object is a string or symbol, and each object data value can be in any type of data format, including user-defined other object or array elements.

Key Features of Object Data Type in JavaScript.

  • An object created in a JavaScript program is an unordered data element collection of key-value pair properties and their corresponding values.
  • You use the dot notation object.property or the bracket notation object[‘property’] to access declared JavaScript object data properties.
  • Whereas declared object data type properties are dynamic, where existing object data types can be added, updated, and deleted as and when required.

Creating an object data type in JavaScript.

You can create the desired object data type using curly braces {} in a JavaScript program, and manually define object properties within the created object data type.

Example of object data type in JavaScript.

let employee = {

  empname: “Rock”,      // here we declare Property “empname” with value “Rock”

  age: 44,            // here employee Property “age” with value 44

  cont: 9414000000,

  isEmployee: false,    // here boolean Property “isEmployee” define with false value

  address: {           // here list of Nested object created

    city: “Mumbai”,

    country: “India”

  },

  display: function() {  // here we created display Method function as a property

    console.log(“Welcome, ” + this.empname);

  }

};

console.log(employee.empname);  // result is – rock

console.log(employee[‘age’]);  // result is – 44

console.log(employee[‘cont’]);  // result is – 9414000000

// let Accessing nested object

console.log(employee.address.city);  // result is – mumbai

// let here we Calling a method (function within object)

employee.display();  // result is – welcome, rock

Updating and adding object properties in JavaScript.

You can add new object properties or change existing object properties in your created object program.

employee.age = 43;  // let Update the employee “age” property

employee.country = “Japan”;  // let Add a new employee country property “country”

console.log(employee.age);  // result is – 43

console.log(employee.country);  // result is – Japan

Deleting object properties in JavaScript.

Here you can delete object properties using delete operator in the current JavaScript program.

delete employee.country;

console.log(employee.country);  // result is – undefined (property removed)

Accessing and modifying nested objects in JavaScript programs.

You can access nested object properties in the created object program, or modify existing object properties.

employee.address.city = “London”;

console.log(employee.address.city);  // result is – London

Array in JavaScript.

Array is a special type of homogeneous data type object structure in JavaScript programming, array is used in JavaScript program to store contiguous collection of variable values. You can store any type of array element in the created array program, which can contain many other arrays or object properties. Where array in secondary storage location is indexed or arranged in number of array end element order starting from integer value 0.

Key features of JavaScript array.

  • Arrays declared in JavaScript programs are stored in continuous ordered order, which means array elements have a particular specific storage index location address.
  • You can access stored array elements by using declared array index location e.g., array[0].
  • Arrays declared in JavaScript programs are mutable in nature, which means you can modify the created array elements as and when required, by adding array elements, or deleting existing array elements.
  • There are many built-in methods that can be used to add, delete, and iterate over array elements in JavaScript programs.

Creating JavaScript arrays.

You can create arrays by using square brackets [] in JavaScript programs.

let os = [“Windows”, “Mac Os”, “Android”, “Linux”];

console.log(os[0]);  // Result is – windows

console.log(os[1]);  // Result is – mac os

console.log(os[2]);  // Result is – android

console.log(os[3]);  // Result is – linux

Updating and adding array elements in JavaScript.

You can update or add new elements to arrays by using index location in JavaScript programs.

os[1] = “macOS 15 Sequoia”;  // here we Change the second element to “macOS 15 Sequoia”

console.log(os[1]);  // Result is – macOS 15 Sequoia

os.push(“Windows 11”);  // here we Add new element to the end of array

console.log(os);  // Result is – [“Windows”, “Mac Os”, “Android”, “Linux” “Windows 11”]

Deleting array elements in JavaScript.

You can use array methods such as pop(), shift(), and splice() to delete array elements.

os.pop();  // let Removes the last element (“Windows 11”)

console.log(fruits);  // Result is – [“Windows”, “Mac Os”, “Android”, “Linux”]

os.shift();  // here we Removes the first element of array (“Apple”)

console.log(os);  // Result is – [“Mac Os”, “Android”, “Linux”]

Iterating over arrays in JavaScript.

You can use a for each loop to iterate over array elements.

for (let p = 0; p < fruits.length; p++) {

  console.log(os[i]);  // Result is – os

}

os.forEach(function(os) {

  console.log(os);  // Outputs each os using forEach method

});

Nested arrays in JavaScript.

Arrays in JavaScript programs can contain other arrays as nested arrays, making it easier to represent complex array data structures.

Examples of using objects and arrays together.

let multiDimensonArray = [[3, 4], [4, 2], [7, 9]];

console.log(multiDimensonArray[0]);  // result is – [3, 4]

console.log(multiDimensonArray[0][1]);  // result is – 4

console.log(multiDimensonArray[1][1]);  // result is – 2

console.log(multiDimensonArray[2][1]);  // result is – 9

Main Differences Between Objects and Arrays data type.

Featurejavascript ObjectsJavascript Arrays
StructureObject is a Collection of key-value pairs unique valuesArray is a Ordered collection of elements
KeysObject Keys are usually strings or symbolsArray element Indexed by integers (0, 1, 2, …)
OrderObject are Order of keys is not guaranteed (ES6 added insertion order)Array store Order is guaranteed in sequence
AccessWe can Access values using special keys (e.g., obj.key)Array Access values using indices (e.g., arr[0])
Use CaseObject Used for representing structured data (e.g., user profiles, configurations)Array Used for ordered lists (e.g., list of items)
MethodsObject Can have methods (functions as properties)Array Many built-in array methods (e.g., push(), pop(), map())

You can use arrays within objects or objects within arrays to represent complex data in JavaScript programs.

Example of object inside an array 1.

let employee = [

  { name: “Rock”, age: 41, contact:9414 },

  { name: “Mathew”, age: 42 },

  { name: “David”, age: 44 }

];

console.log(employee[1].name);  // result is – mathew

console.log(employee[1].age);  // result is – 42

console.log(employee[0].contact);  // result is – 9414

Example of array inside an object 2.

let employee = {

  hr: “David”,

  person: [“Rock”, “John”, “Harry”]

};

console.log(employee.person[1]);  // Result is – john

Summary of array and object data types in JavaScript.

  • Object data type – Object data type is a unique storage collection of key-value pairs in JavaScript programming. The created object values ​​can be of any type, which can include array elements and other object key-pair values. It is helpful for representing structured data like user profile, configuration settings etc. in JavaScript programs.
  • Arrays data type – Array is a continuous ordered collection of arrays in JavaScript programs. Array data type can store a single type of data element and multiple group data type values. It is helpful for representing a list or index of data in JavaScript.