Working with Arrays and Objects in Javascript


Arrays in JavaScript are a data structure that allows you to store multiple values in a single variable. The values can be of any type, including numbers, strings, or even other arrays.

To define an array, we can either use brackets or object notation.


let studentsArr = ["Manisha", "Giridhar", "Shivakumar", "Seetaram"];
let studentIds = new Array(121,122,123,124,125,126);

console.log("Students IDs");

for(let i=0;i<studentIds.length;i++){

console.log("Students Array");
for(let key in studentsArr){

There are several built-in methods for manipulating arrays, such as push(), pop(), shift(), and unshift(), that allow you to add or remove items from the array.

Using the above methods we can use the arrays as Stack, & Queue.

Below are some examples:

 * The stack is a linear data structure  
 * that is used to store the collection of objects.
 * It is based on Last-In-First-Out (LIFO).

//Let us take the example of Received Phone calls

let receivedCallsStack = [];

receivedCallsStack.push("First Call");
receivedCallsStack.push("Second call");
receivedCallsStack.push("Third call");
receivedCallsStack.push("Fourth call");

for(let key in receivedCallsStack){

console.log("Most recent call is from: "+receivedCallsStack.pop());
console.log("Second most call is from: "+receivedCallsStack.pop());
 * A queue is a linear data structure that stores the elements sequentially.
 * It uses the FIFO approach (First In First Out) for accessing elements.

// Let us take the example of Cashier line in a store

class CashierLine {
    constructor() {
        this.customers = [];
    // Customer stands in the line
    standInQue(customer) {
        return this.customers.push(customer);
    // Customer moves out from the Line
    moveOut() {
        if(this.customers.length > 0) {
            return this.customers.shift();
    // view the last person in the queue
    peek() {
        return this.customers[this.customers.length - 1];
    // check if any customer is present in the line
       return this.customers.length == 0;
    // the size of the queue
        return this.customers.length;
    // empty the queue
        this.customers = [];

let cashierLineQueue = new CashierLine();

console.log("Customers in the queue: ");
console.log("Last person in the queue: ");
console.log("Check if the queue is empty: "+cashierLineQueue.isEmpty());

console.log("No of people in the line: "+cashierLineQueue.size());

console.log("No of customers after the queue is cleared: "+cashierLineQueue.customers.length);


JavaScript objects are key-value pairs that represent data in a structured manner. They allow developers to store and manipulate data in a structured and organized manner. An object in JavaScript is a collection of key-value pairs, where each key represents a property of the object, and its corresponding value represents the data associated with that property.

There are two ways to create an object in JavaScript: using object literals and using the object constructor.

let person = {
  name: "John Doe",
  age: 30,
  address: {
    street: "123 Main St",
    city: "New York"
let person = new Object(); = "John Doe";
person.age = 30;
person.address = {
  street: "123 Main St",
  city: "New York"

In conclusion, objects are a powerful feature of JavaScript and allow developers to store and manipulate data in a structured and organized manner.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.