collections-and-loops-slides
collections-and-loops-slides
David Tucker
CTO Consultant
@_davidtucker_ | davidtucker.net
Array
An Array in JavaScript is an ordered collection of
items. It has a length property and methods for
manipulating the list. Arrays should be used for
collections where the order matters.
arrays.js
// Updated length
console.log(`Length: ${departments.length}`); // 3
Map
A Map is a JavaScript collection type that allows you
to use any data type as your key. It has a size property
and methods for manipulating the collection.
maps.js
// Creating a Map
let accessCodes = new Map();
// Populating a Map
accessCodes.set("employee1234", "8303");
accessCodes.set("employee1235", "1111");
Using Maps console.log(accessCodes.size); // 2
// Creating a Set
let agileTeam = new Set();
// Populating a Set
Using Sets agileTeam.add("employee1234");
agileTeam.add("employee1235");
console.log(agileTeam.size); // 2
// While Loop
let x = 10;
While Loops while(x > 0) {
console.log(x);
--x;
}
// 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
do-while.js
// Do-while Loop
let x = 10;
Do While Loops do {
console.log(x);
--x;
} while(x > 0);
// 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
for.js
// For Loop
let departments = ["Marketing", "Engineering", "HR"];
for(let i = 0; i < departments.length; i++) {
console.log(departments[i]);
}
// For...in Loop
let obj = { firstName: "David", lastName: "Tucker" };
for(let key in obj) {
console.log(`${key}: ${obj[key]}`);
}
The break keyword allows
us to terminate the loop
and move to the next
statement in our code.
The continue keyword
stops the current execution
of the loop and proceeds to
the next iteration.
While Loops
Up Next:
For Loops
For Loops
Loop Control Flow
Reading CLI Arguments
Demo
Reading command line arguments passed
to our application
Utilizing loops to display information for
multiple employees