• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Create Array of Zeros in JavaScript

Create Array of Zeros in JavaScript

June 2, 2022 Leave a Comment

In JavaScript, we can easily create an array of zeros. The easiest way to create an array of zeros only is to use the javascript array fill() method.

var array_of_zeros = Array(10);
array_of_zeros.fill(0);

console.log(array_of_zeros);

#Output:
[0,0,0,0,0,0,0,0,0,0]

A second way to make an array of zeros in JavaScript is to use a for loop.

var array_of_zeros = [];
for ( var i=0; i<arraySize; i++; ){
  array_of_zeros[i] = 0;
}

In the code above, the variable arraySize is however long you want the array to be. So let’s say we wanted an array of size 10 containing all zeros.

var array_of_zeros = [];
for ( var i=0; i<10; i++ ){
  array_of_zeros[i] = 0;
}
console.log(array_of_zeros);

#Output:
[0,0,0,0,0,0,0,0,0,0]

In JavaScript, creating an array of zeros can be useful if we want to initialize an array to count or fill later on in our program.

There are a number ways that we can create and fill an array with zeros.

The easiest way to create an array with only zeros is by using the fill() method.

Once again, here is our code to create an array of zeros:

var array_of_zeros = Array(10);
array_of_zeros.fill(0);

You can use this method to create an array that contains any value as shown below in JavaScript.

var array_of_a = Array(10);
array_of_a.fill('a');

console.log(array_of_a);

#Output:
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']

Create a function that returns an Array of Zeros in JavaScript at a Specific Size

To make things easier for us, we will take our code from above and put it in a function. The function will take one value, an integer, that will be the size of the array we want and fill it with zeros.

function arrayOfZeros(arr_size){
  var array_of_zeros = Array(arr_size);
  array_of_zeros.fill(0);

  return array_of_zeros;
};

And here is our function in action:

function arrayOfZeros(arr_size){
  var array_of_zeros = Array(arr_size);
  array_of_zeros.fill(0);

  return array_of_zeros;
};

console.log(arrayOfZeros(5));

#Output:
[0,0,0,0,0]

Create a function that returns an Array of any Values at a Specific Size

We can also improve upon our previous function to make an array containing anything at a specific size.

function arrayOfAnything(arr_size,arr_value){
  var array_of_anything = Array(arr_size);
  array_of_anything.fill(arr_value);

  return array_of_anything;
};

And here is our function in action:

function arrayOfAnything(arr_size,arr_value){
  var array_of_anything = Array(arr_size);
  array_of_anything.fill(arr_value);

  return array_of_anything;
};

console.log(arrayOfAnything(7,"hello"));

#Output:
['hello', 'hello', 'hello', 'hello', 'hello', 'hello', 'hello']

Hopefully this article has been useful for you to learn how to use JavaScript to make an array of zeros.

Other Articles You'll Also Like:

  • 1.  JavaScript Coin Flip – How to Simulate Flipping a Coin in JavaScript
  • 2.  Using JavaScript to Reverse an Array
  • 3.  Using JavaScript to Check if Variable is an Object
  • 4.  JavaScript asinh – Find Hyperbolic Arcsine of Number Using Math.asinh()
  • 5.  Using JavaScript to Round to 1 Decimal
  • 6.  Using JavaScript to Check if Array is Empty
  • 7.  Using JavaScript to Get Radio Button Value
  • 8.  Using JavaScript to Detect Window Resize
  • 9.  Get the First Child Element and Change it with JavaScript
  • 10.  How to Check if a Number is Even or Odd in JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy