curriculum/challenges/english/blocks/lecture-working-with-arrays-variables-and-naming-practices/6732c6ba2ea42b610b9f9ce1.md
In prior lessons you were first introduced to the length property, this property returns the number of elements in an array. So here is a quick reminder on how it works:
const fruits = ['apple', 'banana', 'orange'];
console.log(fruits.length); // 3
It's possible to have arrays with empty slots. Empty slots are defined as slots with nothing in them. This is different than an array with the value of undefined. These types of arrays are known as sparse arrays. Here is an example:
const sparseArray = [1, , , 4];
console.log(sparseArray.length); // 4
In this case even though we only have two defined elements, 1 and 4, the length is 4 because the highest index (3) plus 1 gives us a length of 4.
Now let's discuss how to create an empty array of fixed length. There are few ways to do this in JavaScript but one common method is to use the Array() constructor with a numeric argument. The Array() constructor can be used with the new keyword to create a new array. Here is an example:
const emptyArray = new Array(5);
console.log(emptyArray.length); // 5
console.log(emptyArray); // [ , , , , ]
In this example, we create a new array using Array(5). This creates a sparse array with a length of 5 where all the slots are empty.
Another way to create an empty array of fixed length is to use the Array.from() method with a length argument. Unlike new Array(n), this method creates an array of the specified length where all elements exist and have a value of undefined:
const fixedLengthArray = Array.from({ length: 5 });
console.log(fixedLengthArray.length); // 5
console.log(fixedLengthArray); // [undefined, undefined, undefined, undefined, undefined]
If you want to create an array of specific length and fill it with a default value, you can use the Array.fill() method:
const filledArray = new Array(3).fill(0);
console.log(filledArray); // [0, 0, 0]
This creates an array of length three and fills all elements with the value 0. Note: when filling with objects, all slots reference the same object; if you need independent copies, use a callback or Array.from() instead.
Understanding how to get the length of an array and create arrays of fixed length is important for many programming tasks especially when you need to initialize arrays for specific algorithms or data structures.
What will be the output of the following code?
let arr = [1, 2, 3, , 5];
console.log(arr.length);
4
Remember that the length property returns the highest numeric index plus one, even if there are empty slots.
5
3
Remember that the length property returns the highest numeric index plus one, even if there are empty slots.
This will throw an error.
Remember that the length property returns the highest numeric index plus one, even if there are empty slots.
2
Which of the following is a way to create a sparse array?
new Array(5)
Array.from({ length: 5 })
Review the middle of the lesson for the answer.
new Array(5).fill(undefined)
Review the middle of the lesson for the answer.
[1, 2, 3, 4, 5]
Review the middle of the lesson for the answer.
1
What will be the output of the following code?
let arr = new Array(3).fill(1);
console.log(arr);
[1, 1, 1]
[3]
The fill() method fills all the elements of an array with a static value.
[undefined, undefined, undefined]
The fill() method fills all the elements of an array with a static value.
[<3 empty items>]
The fill() method fills all the elements of an array with a static value.
1