Eloquent Javascript Chapters 3 - 5
typeof null
'object'
map method
- applies a function to all array's elements and builds new array map([3, 2, 1], n => n*2); [6, 4, 2];
obj.x vs. obj[x]
- value.x fetches the property "x" - value[x] fetches the property of the value of the variable named x to use dot notation, the name has to be a valid binding name starting with a letter or underscore, and containing only letters, numbers & underscores. You cannot fetch obj.2 or obj."John Doe"
built in array method filter
.filter(e => boolean test). Pure function that builds new array. [3, 2, 1].filter(e => e % 2 == 0); returns [2];
lexical scoping
Each local scope can see all the local scopes that contain it, and all scopes can see the global scope
built in array method reduce
[1, 2, 3, 4].reduce((a, b) => a + b); 10
Call reduce to determine the maximum of an array
[5, 4, 3, 2, 1].reduce( (a, b) => { return a > b ? a : b; });
closure function
a function value that contains its original local bindings, including parameters
optional property access
a?.b means the same as a.b when a isn't null or undefined. When it is, it evaluates to undefined can also be used w/ square bracket access a?.[0]
array destructuring
add bindings for individual array elements function slice([startIndex, endIndex]) { you can do this with any let/var/const bindings, it seems
does the array include the value?
array.includes('value') works for strings and other data structures
slice array
array.slice(startIndex, optional endIndex)
arrow function declaration
const square1 = (x) => { return x * x; }; const square2 = x => x * x; const horn = () => { console.log("Toot"); };
How do each of filter, map & reduce change the length of the returned value?
filter: smaller array map: array of the same size reduce: single value
what's the foreach of JS?
for (let entry of list) {
Higher order function that creates new functions
function greaterThan(n) { return m => m > n; } let greaterThan10 = greaterThan(10); console.log(greaterThan10(11)); // → true
Higher order function that changes other functions
function noisy(f) { return (...args) => { console.log("calling with", args); let result = f(...args); console.log("called with", args, ", returned", result); return result; }; } noisy(Math.min)(3, 2, 1); // → calling with [3, 2, 1] // → called with [3, 2, 1] , returned 1
Object.keys
function that will return an array of property strings
Higher order function that provides new control flow
function unless(test, then) { if (!test) then(); } repeat(3, n => { unless(n % 2 == 1, () => { console.log(n, "is even"); }); });
Object.assign
give an object properties Object.assign(objectA, {b:3, c:4})
Rest parameter, function call or concatenation
let numbers = [3, 5, 7]; max(...numbers) let moreNums = [2, ...numbers, 11]; 'spreads' out the array into the function call
built in array method forEach
little for loop [3, 2, 1].forEach(e => console.log(e));
converting objects/arrays to and from JSON
parse JSON: JSON.parse(string) make string: JSON.stringify(object)
array shift() & unshift(x)
shift: pop off the first item of array and return unshift: pop on a new first item of array
Rest parameter; function definition
three dots before indicate that the last parameter can be any number of arguments function max(...numbers) {
strings properties trim() padStart(3, "0") split(" ") join(". ")
trim: gets rid of whitespace from start and end padStart: desired length and padding as inputs split: array from string join: takes array and returns string
built in array method every
will return true if every item in the array passes the test [23, 22, 20].every(a => a > 18); true
built in array method some
will return true if some of the items in the array pass the test. [5, 4, 3, 2, 1].some(a => a > 18); false
`in`
will tell you if an object has a property ("lastName" in aStudent = false)
get index of an array value
x.indexOf("b") also x.lastIndexOf() both take an optional second argument to indicate which index to start searching