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);

});

Download the notes
Arrays in JavaScript
Download as PDF
Download as PDF

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']

Take a Practice Test
Test yourself on topics from Software Development exam
Practice Now
Practice Now

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
Are you preparing for Software Development Exam? Then you should check out the best video lectures, notes, free mock test series, crash course and much more provided by EduRev. You also get your detailed analysis and report cards along with 24x7 doubt solving for you to excel in Software Development exam. So join EduRev now and revolutionise the way you learn!
Sign up for Free Download App for Free
51 videos|28 docs|12 tests

Up next

51 videos|28 docs|12 tests
Download as PDF

Up next

Explore Courses for Software Development exam
Related Searches

Objective type Questions

,

Free

,

Arrays in JavaScript | JavaScript for Web Development - Software Development

,

Arrays in JavaScript | JavaScript for Web Development - Software Development

,

Previous Year Questions with Solutions

,

study material

,

mock tests for examination

,

past year papers

,

practice quizzes

,

Extra Questions

,

ppt

,

Semester Notes

,

Viva Questions

,

shortcuts and tricks

,

MCQs

,

Sample Paper

,

Summary

,

Important questions

,

video lectures

,

pdf

,

Exam

,

Arrays in JavaScript | JavaScript for Web Development - Software Development

;