How to Print an Array in javascript
To print an array in JavaScript, you can use the console.log() method. This method takes the array as an argument and prints it to the console.
Print an Array of Strings in javascript
To print an array of strings in JavaScript, you can use the console.log() method in a similar way to how you would print an array of simple data types. Here is an example of how to print an array of strings in JavaScript:
let arr = ["apple", "banana", "orange"]; console.log(arr);
This code will print the following to the console:
["apple", "banana", "orange"]
You can also use the console.log() method to print individual elements of the array by providing the index of the element that you want to print. Here is an example:
let arr = ["apple", "banana", "orange"]; console.log(arr[0]); // prints "apple" console.log(arr[1]); // prints "banana" console.log(arr[2]); // prints "orange"
This code will print the first, second, and third elements of the array to the console.
Print an Array of Objects in javascript
In a similar way to how you would print an array of simple data types, to print an array of objects in JavaScript, you can use the console.log() method . Here is an example of how to print an array of objects in JavaScript:
let arr = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 35 }
];
console.log(arr);
This code will print the following to the console:
[
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 35 }
]
You can also access and print individual properties of the objects in the array. Here is an example:
let arr = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 35 }
];
console.log(arr[0].name); // prints "Alice"
console.log(arr[1].name); // prints "Bob"
console.log(arr[2].name); // prints "Charlie"
This code will print the name property of each object in the array to the console.
