49

why is the following code showing undefined? Are we not allowed to create an array with a single value? Putting two values won't show this error. Is this a problem with Javascript?

<script>
var tech = new Array(21);
alert(tech[0]);
</script>
1
  • to understand what happens , console.log(tech.valueOf()); or console.log(tech.toString()); Commented Feb 17, 2017 at 10:26

8 Answers 8

73

new Array(21) creates an array with a length of 21. If you want to create a single-value array, consisting of a number, use square brackets, [21]:

var tech = [ 21 ];
alert(tech[0]);

If you want to dynamically fill an array, use the .push method:

var filler = [];
for(var i=0; i<5; i++){
    filler.push(i); //Example, pushing 5 integers in an array
}
//Filler is now equivalent to: [0, 1, 2, 3, 4]

When the Array constructor receives one parameter p, which is a positive number, an array will be created, consisting of p elements. This feature is can be used to repeat strings, for example:

var repeat = new Array(10);
repeat = repeat.join("To repeat"); //Repeat the string 9x
Sign up to request clarification or add additional context in comments.

4 Comments

This is incorrect. The array doesn't contain any elements, rather it has a length property of that value. There is a difference: a=Array(3); b=[1,2,3]; 1 in b === true; 1 in a === false
@Rob W - but the thing is I am dynamically creating arrays in a loop, some are single valued, some are not. So whats the better way?
You're still incorrect after your edit. They are not initialised as undefined. They simply don't exist.
@ImranOmarBukhsh Use the .push method to fill the array. See my updated answer.
10

You can create an Array with a value using Array.of

let arr = Array.of(8)
console.log(arr)

Comments

8

by new Array(21) you're actually creating an array with 21 elements in it.

If you want to create an array with single value '21', then it's:

var tech = [21];
alert(tech[0]);

Comments

2

guys the answer was as simple as this:

<script>
var tech = new Array();
tech.push(21);
alert(tech[0]);
</script>

1 Comment

I suggest accepting the answer by @zvona as the best answer.
1

The above example defining an array called tech with 21 positions. You have to difine it like that

var tech = new Array('21');
alert(tech[0]);

Comments

0

can use push or use brackets notation , passing single value is like initialize length

var tech = new Array();
tech.push(10);
console.log(tech[0]); 


var tech = new Array(5);
console.log(tech.length);  // length = 5
tech.push(10);  // will add as a 6th element i.e. a[5]
console.log(tech.length);   // length = 6
console.log(tech[0]);   // undefined 
console.log(tech[5]);  // 10 


or the easy way

var tech = [10];
console.log(tech[0]); 

Comments

0

Here is my solution.

    var tech = new Array(); //create an empty array
    tech.push(21); //Append items to the array
    console.log(tech); // console array the array

Be careful to avoid this; var array = new Array(n), this creates an empty array of length n, then if you push; you will simply append the new item to the end of the array and the new array length will be n+1 and your new array will look like this array = [Empty*21,new item ]

Comments

0

A possible solution to create an array using the Array constructor and having only one number element can be by using the fill method:

new Array(1).fill(4)// [ 4 ]

It will fill all the elements of the constructed array with a static value (4 in our case).

Note that fill() will only work in browsers with ES6 support.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.