Eloquent JavaScript by Marijn Haverbeke

Eloquent JavaScript

7.4/107 min read

8/29/17

  • 6 basic types of values: numbers, strings, booleans, objects, functions, and undefined

  • Infinity and NaN (not a number) are both numbers

  • NaN !== NaN

  • Typeof: get the type of a value or variable. Ex. typeof 4.5, typeof 'x', typeof x

  • Comparison of alphabetic strings is by unicode value: 'Z' < 'a', 'a' < 'z'

  • Null and undefined are values but carry no information

  • Null == undefined, null != 0

  • 0, Nan, and '' count as false, 0 == false, '' == false

  • Us break; to jump out of a loop

  • Use continue; to go to the next iteration of a loop

  • Lexical scope: all variables surrounding a local scoped function are available in the function

  • In JavaScript, functions are the only things that create a new scope, not blocks

  • Let creates a variable that is local to the enclosing block, not the enclosing function

  • Function variables can be passed around. Ex: let square = function(x) { return x * x; };

  • Function declarations define the variable and point it at the function. The variables are moved to the top of their scope. Ex. function square(x) { return x * x; };

  • Function calls get put in a call stack

  • Closure: a function that closes over local variables. A function within another function. The inner function remembers the environment in which it was created and has access to the outer function's variables and parameters. The function 'freezes' the code in its body and wraps it into a package

  • Recursion: a function that calls itself

  • Running through a loop is a lot cheaper than calling a function multiple times

  • Pure function: when called with the same arguments, it always produces the same value, zero side effects

  • Dot notation allows us to access the property of a value

  • Reading a property that doesn't exist will produce the value undefined

  • Delete anObject.left will remove a named property from the object

  • Numbers, strings, and booleans are all immutable

  • == operator will return true only if both objects are precisely the same value. Comparing different objects will return false, even if they have identical contents

  • For (let event in map) maps over the keys in a map

  • Array: length, push, pop, unshift, shift, indexOf, lastIndexOf, slice(start, end), concat

  • String: slice(start, end), indexOf, trim, length, charAt

  • A special arguments variable/object is added to the function environment whenever a function is called. arguments.length is the # of parameters passed into the function

  • Math: random (a random number between 0 and 1), floor, ceil, round

  • ForEach is a standard method on arrays. Only takes one argument, the function to be executed for each element

  • Higher-order functions: functions that operate on other functions, either by taking them as arguments or returning them

  • Filter: returns a new array of values that pass the test

  • Map: transforms the array by applying a function to all of its elements and builds a new array from the returned values

  • This: special variable which refers to the object it is called on

  • Prototype: the original type of an object which has properties. Object.prototype is the ancestral prototype behind all objects

  • Implement defensive programming to ignore nonsense input

  • Throw new Error('Something went wrong.');

  • Try { ... } catch(error) { .... };

  • Tye/finally or try/catch/finally: finally runs no matter what, usually contains clean up

  • Regular expressions: new RegExp('abc'); or /abc/;

  • Put a backslash before any character that isn't a letter, number, or whitespace

  • Characters between square brackets match any of the characters: /[0123456789]/.test(' in 1993') // true. Within square brackets, a dash (-) between characters represents a range

  • Common character groups: d (any digit, [0-9]), w (any alphanumeric character), s (any whitespace character, space, tab, newline), D (any char not a digit), W (non-alphanumeric character), S (non-whitespace character), . (any character except for newline)

  • Using a carat (^) after the opening square bracket is invert or except

  • A plus sign after something in a regular expression indicates the element may be repeated more than once

  • The star (*) means the same as + but also may match zero times

  • Question mark means optional

  • Curly braces indicate that a pattern should occur a precise number of times, can also use a range, ranges may be open ended

  • I: makes the regular expression case insensitive

  • ^ matches the start of the string, $ matches the end

  • : is a word boundary, word must start or end with a nonword character

  • Pipe (|) represents a choice between the pattern on the left or right

  • g means all matches will be replaced

  • The search method is like indexOf

  • Month numbers start at 0 for JavaScript Date, but days start at 1

  • Date: getTime, getFullYear, getMonth, getDate, getHours, getMinutes, getSeconds

  • Node.js is a browserless JavaScript environment

  • Modules create an interface for external code to use

  • Functions are the only things in JavaScript that create a new scope

  • Network protocol: style of communication over a network

  • TCP: transmission control protocol, most communication on the internet is built on this

  • HTTP: hypertext transfer protocol allows computers to request documents over the network

  • Each document on the web is named by a url (uniform resource locator) made up of a protocol, server, and path

  • Each machine connected to the internet gets a unique IP address

  • HTML (hypertext markup language) is the document format used for web pages

  • Href: hypertext reference

  • Each document node has a parentNoode, childNodes, firstChild, lastChild, previousSibling, nextSibling, nodeType, and nodeValue

  • OffsetWidth and offsetHeight give you the space the element takes up

  • ClientWidth and clientHeight is the size of space iinside the element, ignoring the border

  • GetBoundingClientRect: returns an object with top, bottom, left, and right properties giving the pixel positions of the sides of the element relative to the top left of the screen

  • Browsers do not update their display when a JavaScript program is running

  • Browsers allow us to register functions as handlers for specific events

  • Event handler functions are passed an event object as an argument. It has information about the event. Events have a target property which refers to the node it was originated

  • Modifier keys: shiftKey, ctrlKey, altKey, metaKey

  • Whenever an element is scrolled, a 'scroll' event fires on it

  • InnerHeight global variable gives us the height of the window

  • When a page finishes loading, the 'load' event fires on the window and document objects

  • When a page is navigated away from, a 'beforeunload' event fires to prevent losing work

  • Browsers provide web works to do something time-consuming in the background

  • SetTimeout schedules another function to be called later. If you want to cancel a function you have scheduled, store it in a variable and call clearTimeout(variable)

  • HTTP methods: GET, DELETE, PUT, POST

  • Status codes: 200s success, 400s request error, 500s server error

  • A query string on a URL starts with a question mark and contains names and values separated by &s

  • Promises: wrap an asynchronous action in an object, which can be passed around and told to do certain things when the action finishes or fails

  • A button with a type attribute submit will submit forms, same as pressing enter when a form is active

  • Long pulling: client continuously asks the server for new information

  • Web sockets: open connection for data exchange between client and server

  • Router: component that helps dispatch a request to the function that can handle it