0% found this document useful (0 votes)
13 views22 pages

Collections and Loops Slides

Uploaded by

kullamabdullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views22 pages

Collections and Loops Slides

Uploaded by

kullamabdullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Collections and Loops

David Tucker
CTO Consultant

@_davidtucker_ | [Link]
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.
[Link]

// Creating and populating an array


let departments = [ "Marketing", "Engineering" ];

// Accessing array items


[Link](`First Item: ${departments[0]}`);

Using Arrays // Checking the array length


[Link](`Length: ${[Link]}`); // 2

// Add item to the array


[Link]("Human Resources");

// Updated length
[Link](`Length: ${[Link]}`); // 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.
[Link]

// Creating a Map
let accessCodes = new Map();

// Populating a Map
[Link]("employee1234", "8303");
[Link]("employee1235", "1111");
Using Maps [Link]([Link]); // 2

// Retrieving values from a Map


let code = [Link]("employee1234");
[Link](code); // 8303

// Delete values from a Map


[Link]("employee1235");
[Link]([Link]); // 1
Set
A Set in JavaScript is a collection type that allows for
a unique set of values. It has a size property and
methods for manipulating the set.
[Link]

// Creating a Set
let agileTeam = new Set();

// Populating a Set
Using Sets [Link]("employee1234");
[Link]("employee1235");
[Link]([Link]); // 2

// Trying to re-add a value


[Link]("employee1235");
[Link]([Link]); // 2 (unchanged)
Arrays
Map and Set
Loops
Loop
In JavaScript, a loop executes a block of statements
until a specific condition is met. One such condition
could be reaching the end of a collection, but it also
could be any other conditional you could think of.
[Link]

// While Loop
let x = 10;
While Loops while(x > 0) {
[Link](x);
--x;
}
// 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
[Link]

// Do-while Loop
let x = 10;
Do While Loops do {
[Link](x);
--x;
} while(x > 0);
// 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
[Link]

// For Loop
let departments = ["Marketing", "Engineering", "HR"];
for(let i = 0; i < [Link]; i++) {
[Link](departments[i]);
}

For Loops // For...of Loop


for(let val of departments) {
[Link](val);
}

// For...in Loop
let obj = { firstName: "David", lastName: "Tucker" };
for(let key in obj) {
[Link](`${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

You might also like