2.1 JavaScript Data Types
# Review
JavaScript Fundamentals - Data Types (opens new window)
The seven "primitive" data types
- number
- bigint
- boolean
- string
- null
- undefined
- symbol
- and hopefully soon
tuples
andrecords
(see reference section below)
Arrays
Store an ordered collection of values.
# Array methods (opens new window)
- Array.forEach()
- Array.filter()
- Array.map()
- Array.reduce()
- Array.find()
- Array.findIndex()
- Array.includes()
- Array.some()
- Array.every()
- Array.indexOf()
- Array.sort()
Objects
Store a keyed collection of values.
# Object methods (opens new window)
- Object.keys()
- Object.values()
- Object.entries()
# What is this
?
this
with functions, arrow functions, and lexical scope GIST (opens new window)
# Map and Set (opens new window)
- what
- when
- how
# Map examples
let m = new Map();
m.set(key, value);
m.has(key);
m.get(key);
m.delete(key);
let arrK = m.keys();
let arrV = m.values();
m.clear();
let lenM = m.size;
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# Set examples
let s = new Set();
s.add(value);
s.has(value);
s.delete(value);
s.clear();
let lenS = s.size;
1
2
3
4
5
6
2
3
4
5
6
See below for video about maps and sets
# Practice exercises
- Write a function that will evaluate the given input string and return a new string containing the characters that appear in the source string only once.
Solution Code
function findSingleChars(source) {
const charMap = new Map();
// Iterate over the characters in the source string
for (let char of source) {
// Use the `.get()` method to obtain the current count for the `char`
// If it is not yet in the map, it will return undefined, so set zero
let charCount = charMap.get(char) || 0;
// Increment the count for the current `char`
charMap.set(char, ++charCount);
}
// Loop over the members of the `charMap` and if their count is one,
// append them to the `unique` return string.
let unique = '';
charMap.forEach((value, key) => {
if (value === 1) unique += key;
});
return unique;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- Write a function that given a source string, returns a new string containing all of the unique characters which appear in the source string at least once.
Solution Code
function findUniqueCharacters(source) {
const characters = new Set();
// Iterate over the characters in the source string, adding them to the set
for (let char of source) {
characters.add(char);
}
// Copy the members of the set into a new array using the spread operator
// and then join the elements to create a string.
return [...characters].join('');
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Because the Set()
constructor function can accept any JavaScript iterable (opens new window) object, and strings are iterable, we can pass the source string directly when creating the Set. The above solution can then be done as a one liner arrow function. This time, instead of using the spread operator, use the Array.from() (opens new window) method.
See the Iterable vs Enumerable video in the References section.
const altSolution = (sourceString) =>
Array.from(new Set(sourceString)).join('');
1
2
2
# Before next week
TODO
Start the first hybrid assignment.