JavaScript 2022

Term 3 Wednesday

May - July 2022

This site will be updated live during the sessions

Week 2 Examples 2

		
001//create an array of 3 cities
		
002let cities = ['Athens' , 'Paris', 'London'];
		
003//log to console just Paris
		
004console.log(cities[1]);
		
005//Add New York
		
006//i've used .length here to find the next index 
		
007//remember, the last index is always length -1
		
008cities[cities.length] = 'New York';
		
009//log to the console the length of the array
		
010console.log("Length after New York is added:");
		
011console.log(cities.length);
		
012cities[cities.length] = 'Tokyo';
		
013console.log("Length after Tokyo is added:");
		
014console.log(cities.length);
		
015
		
016console.log(cities);
		
017for(let i = 0; i < cities.length; i++)
		
018{
		
019	console.log(cities[i]);
		
020}
		
021
		
022for (let i = 0; i < cities.length; i++)
		
023
		
024{
		
025
		
026console.log("City " + (i+1) + ': ' +   cities[i]);
		
027
		
028}