console.log(person.firstName + " is " + person.age + " years old and he's eye color is " + person.eyeColor)
Output:
John is 50 years old and he's eye color is blue
JavaScript Arrays
An array is a special variable, which can hold more than one value:
const cars = ["Saab", "Volvo", "BMW"];
Why we use Arrays?
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this
let car1 = "Saab";
let car2 = "Volvo";
let car3 = "BMW";
However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?, The solution is an array! An array can hold many values under a single name, and you can access the values by referring to an index number.
Creating an Array
Using an array literal is the easiest way to create a JavaScript Array
Syntax:
const array_name = [item1, item2, ...]; //It is a common practice to declare arrays with the const keyword.
Example 1:
const cars = ["Saab", "Volvo", "BMW"];
console.log(cars);
Output:
[ 'Saab', 'Volvo', 'BMW' ]
Note:
The first element of the Array is always considered as 0
Example 2:
const cars = ["Saab", "Volvo", "BMW"];
console.log(cars[0]);
console.log(cars[1]);
console.log(cars[2]);
Output:
Saab
Volvo
BMW
You can also create an array, and then provide the elements: