$100 Website Offer

Get your personal website + domain for just $100.

Limited Time Offer!

Claim Your Website Now

JavaScript Arrays

In JavaScript, an array is a special type of object used to store multiple values within a single variable. Arrays can contain elements of any data type, including numbers, strings, objects, functions, or even other arrays, and they can dynamically grow or shrink in size as needed.

Here’s how you can create and work with arrays in JavaScript:

Creating Arrays:

// Array literal syntax
let fruits = ['apple', 'banana', 'orange'];

// Using the Array constructor
let cars = new Array('Toyota', 'Honda', 'BMW');

// An array can hold elements of different types
let mixedArray = [1, 'two', true, { name: 'John' }];

Accessing Array Elements:

console.log(fruits[0]); // Output: 'apple'
console.log(cars[1]);   // Output: 'Honda'

Modifying Array Elements:

fruits[1] = 'grape';    // Change the value at index 1 to 'grape'
console.log(fruits);    // Output: ['apple', 'grape', 'orange']

Array Length:

console.log(fruits.length); // Output: 3

Adding Elements to an Array:

fruits.push('kiwi');     // Adds 'kiwi' to the end of the array
console.log(fruits);     // Output: ['apple', 'grape', 'orange', 'kiwi']

fruits.unshift('lemon'); // Adds 'lemon' to the beginning of the array
console.log(fruits);     // Output: ['lemon', 'apple', 'grape', 'orange', 'kiwi']

Removing Elements from an Array:

fruits.pop();      // Removes the last element ('kiwi')
console.log(fruits); // Output: ['lemon', 'apple', 'grape', 'orange']

fruits.shift();    // Removes the first element ('lemon')
console.log(fruits); // Output: ['apple', 'grape', 'orange']

Iterating Over Arrays:

fruits.forEach(function(fruit) {
    console.log(fruit);
});

Related Posts

The Comprehensive Blueprint for Free Vehicle Rental Management Software

Introduction The growth of the global vehicle rental industry has been fueled by a shift in consumer behavior toward the sharing economy. As travel demands rise and…

Read More

Comparing the World’s Best Heart Surgery Hospitals and Clinical Teams

Introduction The Growing Burden of Cardiovascular Diseases Cardiovascular diseases (CVDs) remain the leading cause of mortality globally, placing an unprecedented burden on healthcare delivery systems. Ischemic heart…

Read More

Affordable Website Design for Growing Companies: The Complete Strategic Guide

Introduction In the digital landscape, a website serves as a company’s modern storefront, primary lead generator, and foundational brand asset. For growing companies, startups, and small businesses,…

Read More

The Ultimate Guide to a User-Friendly and Mobile-Ready Business Website

Introduction A corporate website is no longer just a passive digital brochure; it functions as your primary storefront, lead generator, and brand ambassador. Over 60% of all…

Read More

Best Places to Visit in Goa: The Ultimate Travel Guide to Beaches, Nightlife & Itineraries

Introduction Finding the best places to visit in Goa means opening the door to a paradise that seamlessly blends laid-back coastal living with high-octane excitement. Whether you…

Read More

Best Countries for Plastic Surgery: Top Global Cosmetic Hospitals, Surgeons, and Costs

Introduction Deciding to undergo an aesthetic procedure is a deeply personal journey, and finding the best cosmetic hospitals in the world is the critical first step toward…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x