Skip to main content
PHP Blog

Back to all posts

How to Fill an Array With A For Loop In JavaScript?

Published on
5 min read
How to Fill an Array With A For Loop In JavaScript? image

Best JavaScript Programming Books to Buy in October 2025

1 JavaScript: The Definitive Guide: Master the World's Most-Used Programming Language

JavaScript: The Definitive Guide: Master the World's Most-Used Programming Language

BUY & SAVE
$34.30 $79.99
Save 57%
JavaScript: The Definitive Guide: Master the World's Most-Used Programming Language
2 JavaScript from Beginner to Professional: Learn JavaScript quickly by building fun, interactive, and dynamic web apps, games, and pages

JavaScript from Beginner to Professional: Learn JavaScript quickly by building fun, interactive, and dynamic web apps, games, and pages

BUY & SAVE
$22.49 $39.99
Save 44%
JavaScript from Beginner to Professional: Learn JavaScript quickly by building fun, interactive, and dynamic web apps, games, and pages
3 Eloquent JavaScript, 4th Edition

Eloquent JavaScript, 4th Edition

BUY & SAVE
$29.79 $49.99
Save 40%
Eloquent JavaScript, 4th Edition
4 JavaScript QuickStart Guide: The Simplified Beginner's Guide to Building Interactive Websites and Creating Dynamic Functionality Using Hands-On Projects (Coding & Programming - QuickStart Guides)

JavaScript QuickStart Guide: The Simplified Beginner's Guide to Building Interactive Websites and Creating Dynamic Functionality Using Hands-On Projects (Coding & Programming - QuickStart Guides)

BUY & SAVE
$34.99
JavaScript QuickStart Guide: The Simplified Beginner's Guide to Building Interactive Websites and Creating Dynamic Functionality Using Hands-On Projects (Coding & Programming - QuickStart Guides)
5 JavaScript: The Comprehensive Guide to Learning Professional JavaScript Programming (Rheinwerk Computing)

JavaScript: The Comprehensive Guide to Learning Professional JavaScript Programming (Rheinwerk Computing)

BUY & SAVE
$39.21 $59.95
Save 35%
JavaScript: The Comprehensive Guide to Learning Professional JavaScript Programming (Rheinwerk Computing)
6 JavaScript Crash Course: A Hands-On, Project-Based Introduction to Programming

JavaScript Crash Course: A Hands-On, Project-Based Introduction to Programming

BUY & SAVE
$30.28 $49.99
Save 39%
JavaScript Crash Course: A Hands-On, Project-Based Introduction to Programming
+
ONE MORE?

To fill an array with a for loop in JavaScript, you can follow these steps:

  1. Create an empty array that you want to fill.
  2. Determine the length of the array you want to create and assign it to a variable.
  3. Use a for loop to iterate over each index of the array.
  4. Inside the loop, use the index to assign values to each element of the array.
  5. The values can be assigned using any logic or algorithm you desire.
  6. After the loop ends, the array will be filled with the desired values based on the logic implemented within the loop.

Remember, with each iteration of the loop, you have the ability to manipulate the values that are assigned to the elements in the array. The loop will continue until it reaches the specified length of the array.

How do you convert a string to an array using a for loop?

To convert a string to an array using a for loop, you can iterate over each character in the string and push it into an array.

Here's an example in JavaScript:

let str = "Hello"; let array = [];

for (let i = 0; i < str.length; i++) { array.push(str[i]); }

console.log(array);

Output:

[ 'H', 'e', 'l', 'l', 'o' ]

In this example, we declare an empty array array and iterate over the characters of the string str using a for loop. Each character is accessed using the index i and pushed into the array using the push() method. Finally, we print the resulting array.

What is the difference between a for loop and a for...in loop in JavaScript?

In JavaScript, there are two types of for loops: a traditional for loop and a for...in loop.

A traditional for loop is used when you know the exact number of iterations required. It typically consists of three parts: initialization, condition, and an update. Here's the syntax:

for (initialization; condition; update) { // code to be executed }

For example, a traditional for loop to print numbers from 1 to 5 would be:

for (let i = 1; i <= 5; i++) { console.log(i); }

On the other hand, a for...in loop is used to iterate over the properties of an object. It iterates over all enumerable properties (including inherited ones) and executes the given code block for each property. Here's the syntax:

for (variable in object) { // code to be executed }

For example, a for...in loop to iterate over the properties of an object and print them would be:

const obj = { prop1: 'value1', prop2: 'value2' };

for (let prop in obj) { console.log(prop, obj[prop]); }

In this example, the loop iterates over prop1 and prop2 properties of the obj object and prints their names and values.

In summary, a traditional for loop is used for known iterations, while a for...in loop is used to iterate over object properties.

How do you filter out specific elements from an array using a for loop?

To filter out specific elements from an array using a for loop, you can follow these steps:

  1. Create an empty array to store the filtered elements.
  2. Iterate over each element in the input array using a for loop.
  3. Inside the loop, add a condition to check if the current element meets the filtering criteria. If it does, add it to the filtered array.
  4. After the loop ends, the filtered array will contain only the elements that pass the filtering criteria.

Here's an example in JavaScript to demonstrate this approach. Let's say we want to filter out all even numbers from an array:

const numbers = [1, 2, 3, 4, 5, 6]; const filteredNumbers = [];

for (let i = 0; i < numbers.length; i++) { const num = numbers[i]; if (num % 2 !== 0) { filteredNumbers.push(num); } }

console.log(filteredNumbers); // prints [1, 3, 5]

In this example, the for loop iterates over each element in the numbers array. The if statement checks if the current number num is not divisible by 2 (i.e., an odd number). If true, the number is added to the filteredNumbers array. Finally, the console.log statement outputs the filtered array.

How do you add elements to an array using a for loop?

To add elements to an array using a for loop, you can follow these steps:

  1. Initialize an empty array.
  2. Use a for loop to iterate over the desired elements.
  3. Inside the for loop, use the array's push() method to add elements to the array.

Here's an example in JavaScript:

// Step 1: Initialize an empty array let myArray = [];

// Step 2: Use a for loop to iterate over elements for(let i = 0; i < 5; i++) { // Step 3: Add elements to the array using push() myArray.push(i); }

console.log(myArray); // Output: [0, 1, 2, 3, 4]

In this example, the for loop iterates from 0 to 4, and each iteration adds the current value of i to the array myArray using the push() method. Finally, the array is printed to the console.

What is the syntax for creating an array in JavaScript?

In JavaScript, an array can be created using either of the following syntax:

  1. Using the array literal syntax:

var array = []; // empty array var array = [1, 2, 3]; // array with initial values

  1. Using the Array constructor:

var array = new Array(); // empty array var array = new Array(1, 2, 3); // array with initial values

Note: It is generally recommended to use the array literal syntax over the Array constructor, as it is more concise and less error-prone in some cases.