Arrays in JavaScript | JavaScript for Web Development - Software Development PDF Download

Introduction

In JavaScript, an array is a data structure that allows you to store multiple values in a single variable. Arrays are incredibly useful for organizing and manipulating collections of data. In this article, we'll explore the basics of arrays in JavaScript, including how to create, access, modify, and iterate over arrays.

Creating Arrays

To create an array in JavaScript, you can use square brackets [] and separate the values with commas. Here's an example:

// Creating an array of numbers

let numbers = [1, 2, 3, 4, 5];

Accessing Array Elements

Array elements are accessed using their index, which starts from 0 for the first element. To access a specific element, you can use the index within square brackets. Here's an example:

// Accessing array elements

console.log(numbers[0]); // Output: 1

console.log(numbers[2]); // Output: 3

Modifying Array Elements

You can modify array elements by assigning new values to specific indices. Here's an example:

// Modifying array elements

numbers[1] = 10;

console.log(numbers); // Output: [1, 10, 3, 4, 5]

Array Length

The length property of an array returns the number of elements it contains. You can access it using the length property. Here's an example:

// Array length

console.log(numbers.length); // Output: 5

Iterating over Arrays

There are several ways to iterate over an array in JavaScript. One common approach is to use a for loop along with the array's length property. Here's an example:

// Iterating over an array using a for loop

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

  console.log(numbers[i]);

}

Another way to iterate over an array is to use the forEach method, which calls a function for each element in the array. Here's an example:

// Iterating over an array using forEach

numbers.forEach(function(element) {

  console.log(element);

});

Sample Problems


1. Write a function that takes an array of numbers as input and returns the sum of all the numbers.

function calculateSum(numbers) {

  let sum = 0;

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

    sum += numbers[i];

  }

  return sum;

}


console.log(calculateSum([1, 2, 3, 4, 5])); // Output: 15

2. Write a function that takes an array of strings as input and returns a new array with all the strings converted to uppercase.

function convertToUpperCase(strings) {

  let uppercaseStrings = [];

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

    uppercaseStrings.push(strings[i].toUpperCase());

  }

  return uppercaseStrings;

}


console.log(convertToUpperCase(['hello', 'world'])); // Output: ['HELLO', 'WORLD']

Conclusion

Arrays are a fundamental data structure in JavaScript that allow you to store and manipulate collections of values. Understanding how to create, access, modify, and iterate over arrays will greatly enhance your ability to work with data in JavaScript. Keep practicing and experimenting with arrays to become more comfortable using them in your code.

The document Arrays in JavaScript | JavaScript for Web Development - Software Development is a part of the Software Development Course JavaScript for Web Development.
All you need of Software Development at this link: Software Development
51 videos|28 docs|12 tests

Top Courses for Software Development

51 videos|28 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Previous Year Questions with Solutions

,

pdf

,

Arrays in JavaScript | JavaScript for Web Development - Software Development

,

Important questions

,

Sample Paper

,

Semester Notes

,

ppt

,

Summary

,

Viva Questions

,

Extra Questions

,

Objective type Questions

,

study material

,

Exam

,

Free

,

mock tests for examination

,

MCQs

,

practice quizzes

,

shortcuts and tricks

,

Arrays in JavaScript | JavaScript for Web Development - Software Development

,

Arrays in JavaScript | JavaScript for Web Development - Software Development

,

past year papers

,

video lectures

;