To access the entire array in JavaScript, simply use the array’s name. This allows you to work with all the elements at once, like printing them out or passing them to a function.
Key Points
- Access by Name: Just use the name of the array to reference it.
- Useful for Functions: You can pass the full array to functions or methods for operations.
Printing the Full Array
let fruits = ['Apple', 'Banana', 'Orange'];
console.log(fruits); // Output: ['Apple', 'Banana', 'Orange']
Here, simply referencing fruits
allows you to see all the elements.
Passing the Full Array to a Function
function printArray(arr) {
console.log(arr);
}
printArray(fruits); // Output: ['Apple', 'Banana', 'Orange']
The entire fruits
array is passed to the function printArray
.
Practical Tips
- Direct Access: Use the array name to see the complete list of elements quickly.
- Functions and Methods: Passing an entire array to a function is useful when performing operations on all items.