Loading, please wait...

A to Z Full Forms and Acronyms

Objects and Arrays of JavaScript

Jul 16, 2020 javascript, objects, arrays, DOM, 2630 Views
There are two significant features of JavaScript, that is Object and Arrays. They are the most important and easy feature of JavaScript which enables the user to write easy readable codes

There are two significant features of JavaScript, that is Objects and Arrays. They are the most important and one of the easy features of JavaScript which enables the user to write easily readable codes and have easy-grip over manipulation of data.

Working with JavaScript Objects.

Objects in JavaScript are like blocks that have sets of properties related to the application we are working on. These properties have two significant part which are

  • The name of the property
  • The value of the property

Like for an instance if there is a program of school records and a set of data is required to store, for that an object can be created defining its properties like class, the number of students and the name of the class teacher, with respect to which there will be sets of values for each property.

var schoolInfo = {               //An object is declared with a variable
class: '9th',               //properties are separated by ‘,’
students: ‘56’,
classteacher: ‘Sophie’
};

This var keyword tells the JavaScript that we are creating a variable names schoolInfo with different sets of properties and each having its own value.

The objects are initialized with “{}” which are also called object initializers.

If the user did not have the data for the object still, the user can define the objects later on with some basic sets of code.

For the start a variable is to be declared with a new function of new Object();  and then by referencing the variable different properties can be inserted.

var schoolInfo = new Object();

schoolInfo.class(//the property name) = ‘9th’ ; (//the value)

schoolInfo.students = ‘56’;

schoolInfo.classteachers =’Sophie’;

The “document” object of JavaScript enables us to interact with these properties and display them in our web-page’s HTML file.

document.write(schoolInfo.class);

There are several different methods to inject properties and for interacting with them. For better guidance look at the MDN Web Docs.

Working with JavaScript Objects.

Any programming language has this property of defining arrays. Basically, arrays are used to define a list of items, each having a unique index number.  An index describes the location of a value stored in memory and it starts from 0. With the help, if these index numbers, a user can retrieve or manipulate the data/list in an array.

Let's suppose there is a list of food items declared in the JavaScript

var food1 = rice;

var food2 = sprouts;

var food3 = sweets;

instead of declaring the list in terms of variable, an array of foodItems can be declared,

var foodItems = [“rice”, “sprouts”, “sweets”];

Spaces and line breaks are not important. A declaration can span multiple lines:

var foodItems = [
“rice”,
“sprouts”,
“sweets”
];

//the list of arrays are defined between “[]”

By refereeing these indexes of list items we can access the array list individually and can perform various functions like displaying the list in the web-page of HTML and even changing the values of the list.

console.log(foodItems[1]); //will display sprouts

There are several different methods for interacting with an array. For better guidance look at the MDN Web Docs.

At last but not the least, JavaScript enables us to make a hybrid structure of arrays and objects, these can be called as Objects as an array. This basically means that inside an object we can have properties set to an array of values.

var schoolInfo = {
class: [“1”, “2”, “3”],
students : [“30”, “45”, “50”]
};

In this method, it is equally easy to access certain values to display, change, or implement and other functions of JavaScript.

console.log(schoolInfo.class[2]);  //will display the value 3
A to Z Full Forms and Acronyms

Related Article