Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default array values

Is there a way to assign a default values to arrays in javascript?

ex: an array with 24 slots that defaults to 0

like image 831
Derek Adair Avatar asked Jan 11 '10 20:01

Derek Adair


People also ask

What are the default values in an array C?

For char arrays, the default value is '\0' . For an array of pointers, the default value is nullptr . For strings, the default value is an empty string "" . That's all about declaring and initializing arrays in C/C++.

What is the default value of an empty array?

Create Empty Array in Java As we already discussed an array which is created and initialized with default values by the compiler is known as empty array. The default values depend on the type of array. For example, default value for integer array is 0 and and 0.0 for the float type.

What is the default value of an element of object type array?

Each array element is initialized with default value of the type default(T) . If it is a reference type then is null , if it is a value type then is 0 for types like int , long , double , and in the case of struct fields are initialized to their default values.

What are the initial values of an int array?

By default, when we create an array of something in Java all entries will have its default value. For primitive types like int , long , float the default value are zero ( 0 or 0.0 ). For reference types (anything that holds an object in it) will have null as the default value.


1 Answers

You can use the fill function on an array:

Array(24).fill(0) 

Note: fill was only introduced in ECMAScript 2015 (alias "6"), so as of 2017 browser support is still very limited (for example no Internet Explorer).

like image 188
Aik Avatar answered Oct 11 '22 12:10

Aik