Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 2, 2024 07:01 am GMT

JavaScript Non-Primitive Data Types

Non-Primitive data types

Non-primitive data types in JavaScript are derived from the primitive data types and are also known as reference data types or derived data types. These data types are stored in the heap memory of the system, unlike primitive data types which are stored in the stack space of the system.

In computer science, an object is a value in memory which is possibly referenced by an identifier. In JavaScript, objects are the only mutable values. Functions are, in fact, also objects with the additional capability of being callable.

const arr = [1,3,4,5];console.log(typeof arr); // objectconst obj = {name:"nick" age: 30};console.log(typeof obj) //object

1. Array

  • JavaScript arrays are written with square brackets.
  • Array items are separated by commas.
  • An array is a special variable, which can hold more than one value.
  • The following code declares (creates) an array called cars, containing three items (car names):
const cars = ["Saab", "Volvo", "BMW"];

Array indexes are zero-based, which means the first item is [0], second is [1], and so on.

You can also create an array, and then provide the elements:

const cars = [];cars[0]= "Saab";cars[1]= "Volvo";cars[2]= "BMW";

Accessing Array Elements:

You access an array element by referring to the index number:

const cars = ["Saab", "Volvo", "BMW"];let car = cars[0];

2. Objects

JavaScript objects are written with curly braces {}.
Objects are variables too. But objects can contain many values.
Object properties are written as name:value pairs, separated by commas.

const person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

The object (person) in the example above has 4 properties: firstName, lastName, age, and eyeColor.

Accessing Object Properties:

You can access object properties in two ways:

objectName.propertyName

or

objectName["propertyName"]

How is an array an 'object'?

As we know, arrays fall in the object category of JavaScript data types i.e they are stored by references and NOT by their actual values.

JavaScript simplifies the work of typeof by returning 'object' whenever the given expression is a reference to some data in memory.

An array is indeed a reference to an ordered bunch of data in memory and likewise translates to 'object' when inspected by the typeof operator.

3 Difference between Array and Object Data Types in JavaScript

In JavaScript, both arrays and objects are used to store and manipulate data, but they have some key differences. Let's explore these differences:

Arrays:

  1. An array is a collection of data stored in a sequence of memory locations.
  2. It can store various data types, including integers, floats, strings, and booleans .
  3. Array elements can be accessed using index numbers, starting from 0.
  4. Arrays are mutable, meaning you can add, remove, and modify elements in an array.
  5. Arrays have built-in methods and properties that allow for easy manipulation and iteration, such as push(), pop(), length, and forEach() .

Objects:

  1. An object is a mutable data structure used to represent a thing or entity.
  2. It stores data in key-value pairs, where the keys can be any string or symbol except for undefined .

Original Link: https://dev.to/imashwani/javascript-non-primitive-data-types-2mjp

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To