JavaScript Notes PDF
Document Details
Uploaded by ExpansiveRomanticism
null
Tags
Summary
This file contains detailed notes on JavaScript, suitable for professionals. It covers a wide range of topics, including console functions, variables, data types, string manipulation, date handling, and more. It's likely to be a helpful source of reference and professional hints and tricks for JavaScript.
Full Transcript
JavaScript JavaScript Notes for Professionals ® Notes for Professionals 400+ pages...
JavaScript JavaScript Notes for Professionals ® Notes for Professionals 400+ pages of professional hints and tricks Disclaimer GoalKicker.com This is an unocial free book created for educational purposes and is not aliated with ocial JavaScript® group(s) or company(s). Free Programming Books All trademarks and registered trademarks are the property of their respective owners Contents About................................................................................................................................................................................... 1 Chapter 1: Getting started with JavaScript..................................................................................................... 2 Section 1.1: Using console.log()..................................................................................................................................... 2 Section 1.2: Using the DOM API.................................................................................................................................... 4 Section 1.3: Using window.alert().................................................................................................................................. 5 Section 1.4: Using window.prompt()............................................................................................................................. 6 Section 1.5: Using window.confirm()............................................................................................................................ 7 Section 1.6: Using the DOM API (with graphical text: Canvas, SVG, or image file)................................................. 8 Chapter 2: JavaScript Variables......................................................................................................................... 10 Section 2.1: Defining a Variable................................................................................................................................. 10 Section 2.2: Using a Variable..................................................................................................................................... 10 Section 2.3: Types of Variables.................................................................................................................................. 10 Section 2.4: Arrays and Objects................................................................................................................................ 11 Chapter 3: Built-in Constants................................................................................................................................ 12 Section 3.1: null............................................................................................................................................................. 12 Section 3.2: Testing for NaN using isNaN().............................................................................................................. 12 Section 3.3: NaN.......................................................................................................................................................... 13 Section 3.4: undefined and null.................................................................................................................................. 14 Section 3.5: Infinity and -Infinity................................................................................................................................. 15 Section 3.6: Number constants.................................................................................................................................. 15 Section 3.7: Operations that return NaN................................................................................................................... 16 Section 3.8: Math library functions that return NaN................................................................................................ 16 Chapter 4: Comments.............................................................................................................................................. 17 Section 4.1: Using Comments..................................................................................................................................... 17 Section 4.2: Using HTML comments in JavaScript (Bad practice)........................................................................ 17 Chapter 5: Console..................................................................................................................................................... 19 Section 5.1: Measuring time - console.time()............................................................................................................ 22 Section 5.2: Formatting console output.................................................................................................................... 23 Section 5.3: Printing to a browser's debugging console......................................................................................... 24 Section 5.4: Including a stack trace when logging - console.trace()..................................................................... 26 Section 5.5: Tabulating values - console.table()...................................................................................................... 26 Section 5.6: Counting - console.count().................................................................................................................... 28 Section 5.7: Clearing the console - console.clear().................................................................................................. 30 Section 5.8: Displaying objects and XML interactively - console.dir(), console.dirxml()...................................... 30 Section 5.9: Debugging with assertions - console.assert()..................................................................................... 32 Chapter 6: Datatypes in JavaScript................................................................................................................. 33 Section 6.1: typeof....................................................................................................................................................... 33 Section 6.2: Finding an object's class........................................................................................................................ 34 Section 6.3: Getting object type by constructor name............................................................................................ 34 Chapter 7: Strings....................................................................................................................................................... 37 Section 7.1: Basic Info and String Concatenation..................................................................................................... 37 Section 7.2: Reverse String......................................................................................................................................... 38 Section 7.3: Comparing Strings Lexicographically................................................................................................... 39 Section 7.4: Access character at index in string....................................................................................................... 40 Section 7.5: Escaping quotes...................................................................................................................................... 40 Section 7.6: Word Counter.......................................................................................................................................... 41 Section 7.7: Trim whitespace...................................................................................................................................... 41 Section 7.8: Splitting a string into an array.............................................................................................................. 41 Section 7.9: Strings are unicode................................................................................................................................. 42 Section 7.10: Detecting a string.................................................................................................................................. 42 Section 7.11: Substrings with slice............................................................................................................................... 43 Section 7.12: Character code...................................................................................................................................... 43 Section 7.13: String Representations of Numbers.................................................................................................... 43 Section 7.14: String Find and Replace Functions...................................................................................................... 44 Section 7.15: Find the index of a substring inside a string....................................................................................... 45 Section 7.16: String to Upper Case............................................................................................................................. 45 Section 7.17: String to Lower Case............................................................................................................................. 46 Section 7.18: Repeat a String...................................................................................................................................... 46 Chapter 8: Date........................................................................................................................................................... 47 Section 8.1: Create a new Date object....................................................................................................................... 47 Section 8.2: Convert to a string format.................................................................................................................... 49 Section 8.3: Creating a Date from UTC..................................................................................................................... 50 Section 8.4: Formatting a JavaScript date............................................................................................................... 53 Section 8.5: Get the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC................................. 55 Section 8.6: Get the current time and date............................................................................................................... 55 Section 8.7: Increment a Date Object....................................................................................................................... 56 Section 8.8: Convert to JSON..................................................................................................................................... 57 Chapter 9: Date Comparison................................................................................................................................ 58 Section 9.1: Comparing Date values.......................................................................................................................... 58 Section 9.2: Date Dierence Calculation.................................................................................................................. 59 Chapter 10: Comparison Operations................................................................................................................ 60 Section 10.1: Abstract equality / inequality and type conversion........................................................................... 60 Section 10.2: NaN Property of the Global Object..................................................................................................... 61 Section 10.3: Short-circuiting in boolean operators................................................................................................. 63 Section 10.4: Null and Undefined............................................................................................................................... 65 Section 10.5: Abstract Equality (==)........................................................................................................................... 65 Section 10.6: Logic Operators with Booleans........................................................................................................... 66 Section 10.7: Automatic Type Conversions............................................................................................................... 67 Section 10.8: Logic Operators with Non-boolean values (boolean coercion)....................................................... 67 Section 10.9: Empty Array........................................................................................................................................... 68 Section 10.10: Equality comparison operations........................................................................................................ 68 Section 10.11: Relational operators (=)........................................................................................................ 70 Section 10.12: Inequality.............................................................................................................................................. 71 Section 10.13: List of Comparison Operators............................................................................................................ 72 Section 10.14: Grouping multiple logic statements................................................................................................... 72 Section 10.15: Bit fields to optimise comparison of multi state data...................................................................... 72 Chapter 11: Conditions.............................................................................................................................................. 74 Section 11.1: Ternary operators.................................................................................................................................. 74 Section 11.2: Switch statement.................................................................................................................................... 75 Section 11.3: If / Else If / Else Control......................................................................................................................... 77 Section 11.4: Strategy................................................................................................................................................... 78 Section 11.5: Using || and && short circuiting............................................................................................................. 79 Chapter 12: Arrays...................................................................................................................................................... 80 Section 12.1: Converting Array-like Objects to Arrays............................................................................................. 80 Section 12.2: Reducing values.................................................................................................................................... 82 Section 12.3: Mapping values..................................................................................................................................... 84 Section 12.4: Filtering Object Arrays.......................................................................................................................... 84 Section 12.5: Sorting Arrays........................................................................................................................................ 86 Section 12.6: Iteration.................................................................................................................................................. 88 Section 12.7: Destructuring an array......................................................................................................................... 92 Section 12.8: Removing duplicate elements............................................................................................................. 93 Section 12.9: Array comparison................................................................................................................................. 93 Section 12.10: Reversing arrays.................................................................................................................................. 94 Section 12.11: Shallow cloning an array..................................................................................................................... 95 Section 12.12: Concatenating Arrays......................................................................................................................... 95 Section 12.13: Merge two array as key value pair.................................................................................................... 97 Section 12.14: Array spread / rest.............................................................................................................................. 97 Section 12.15: Filtering values..................................................................................................................................... 98 Section 12.16: Searching an Array.............................................................................................................................. 99 Section 12.17: Convert a String to an Array............................................................................................................ 100 Section 12.18: Removing items from an array........................................................................................................ 100 Section 12.19: Removing all elements...................................................................................................................... 101 Section 12.20: Finding the minimum or maximum element.................................................................................. 102 Section 12.21: Standard array initialization............................................................................................................. 103 Section 12.22: Joining array elements in a string.................................................................................................. 104 Section 12.23: Removing/Adding elements using splice().................................................................................... 105 Section 12.24: The entries() method........................................................................................................................ 105 Section 12.25: Remove value from array................................................................................................................ 105 Section 12.26: Flattening Arrays.............................................................................................................................. 106 Section 12.27: Append / Prepend items to Array................................................................................................... 107 Section 12.28: Object keys and values to array..................................................................................................... 107 Section 12.29: Logical connective of values........................................................................................................... 108 Section 12.30: Checking if an object is an Array.................................................................................................... 108 Section 12.31: Insert an item into an array at a specific index.............................................................................. 109 Section 12.32: Sorting multidimensional array....................................................................................................... 109 Section 12.33: Test all array items for equality...................................................................................................... 110 Section 12.34: Copy part of an Array...................................................................................................................... 110 Chapter 13: Objects.................................................................................................................................................. 112 Section 13.1: Shallow cloning..................................................................................................................................... 112 Section 13.2: Object.freeze........................................................................................................................................ 112 Section 13.3: Object cloning...................................................................................................................................... 113 Section 13.4: Object properties iteration................................................................................................................. 114 Section 13.5: Object.assign........................................................................................................................................ 115 Section 13.6: Object rest/spread (...)........................................................................................................................ 116 Section 13.7: Object.defineProperty......................................................................................................................... 116 Section 13.8: Accesor properties (get and set)....................................................................................................... 117 Section 13.9: Dynamic / variable property names................................................................................................ 117 Section 13.10: Arrays are Objects............................................................................................................................. 118 Section 13.11: Object.seal........................................................................................................................................... 119 Section 13.12: Convert object's values to array...................................................................................................... 120 Section 13.13: Retrieving properties from an object............................................................................................... 120 Section 13.14: Read-Only property........................................................................................................................... 123 Section 13.15: Non enumerable property................................................................................................................ 123 Section 13.16: Lock property description................................................................................................................. 123 Section 13.17: Object.getOwnPropertyDescriptor................................................................................................... 124 Section 13.18: Descriptors and Named Properties................................................................................................. 124 Section 13.19: Object.keys......................................................................................................................................... 126 Section 13.20: Properties with special characters or reserved words.................................................................. 126 Section 13.21: Creating an Iterable object............................................................................................................... 127 Section 13.22: Iterating over Object entries - Object.entries().............................................................................. 127 Section 13.23: Object.values()................................................................................................................................... 128 Chapter 14: Arithmetic (Math)........................................................................................................................... 129 Section 14.1: Constants.............................................................................................................................................. 129 Section 14.2: Remainder / Modulus (%).................................................................................................................. 129 Section 14.3: Rounding.............................................................................................................................................. 130 Section 14.4: Trigonometry....................................................................................................................................... 132 Section 14.5: Bitwise operators................................................................................................................................ 133 Section 14.6: Incrementing (++)................................................................................................................................ 135 Section 14.7: Exponentiation (Math.pow() or **)..................................................................................................... 135 Section 14.8: Random Integers and Floats............................................................................................................. 136 Section 14.9: Addition (+).......................................................................................................................................... 137 Section 14.10: Little / Big endian for typed arrays when using bitwise operators.............................................. 137 Section 14.11: Get Random Between Two Numbers............................................................................................... 138 Section 14.12: Simulating events with dierent probabilities................................................................................. 139 Section 14.13: Subtraction (-).................................................................................................................................... 140 Section 14.14: Multiplication (*)................................................................................................................................. 140 Section 14.15: Getting maximum and minimum..................................................................................................... 140 Section 14.16: Restrict Number to Min/Max Range................................................................................................ 141 Section 14.17: Ceiling and Floor................................................................................................................................ 141 Section 14.18: Getting roots of a number................................................................................................................ 142 Section 14.19: Random with gaussian distribution................................................................................................. 142 Section 14.20: Math.atan2 to find direction............................................................................................................ 143 Section 14.21: Sin & Cos to create a vector given direction & distance............................................................... 143 Section 14.22: Math.hypot......................................................................................................................................... 144 Section 14.23: Periodic functions using Math.sin.................................................................................................... 145 Section 14.24: Division (/)......................................................................................................................................... 146 Section 14.25: Decrementing (--)............................................................................................................................. 146 Chapter 15: Bitwise operators............................................................................................................................ 148 Section 15.1: Bitwise operators................................................................................................................................. 148 Section 15.2: Shift Operators.................................................................................................................................... 150 Chapter 16: Constructor functions................................................................................................................... 151 Section 16.1: Declaring a constructor function........................................................................................................ 151 Chapter 17: Declarations and Assignments................................................................................................ 152 Section 17.1: Modifying constants............................................................................................................................ 152 Section 17.2: Declaring and initializing constants.................................................................................................. 152 Section 17.3: Declaration........................................................................................................................................... 152 Section 17.4: Undefined............................................................................................................................................. 153 Section 17.5: Data Types........................................................................................................................................... 153 Section 17.6: Mathematic operations and assignment.......................................................................................... 153 Section 17.7: Assignment........................................................................................................................................... 155 Chapter 18: Loops..................................................................................................................................................... 156 Section 18.1: Standard "for" loops............................................................................................................................ 156 Section 18.2: "for... of" loop...................................................................................................................................... 157 Section 18.3: "for... in" loop....................................................................................................................................... 159 Section 18.4: "while" Loops....................................................................................................................................... 159 Section 18.5: "continue" a loop................................................................................................................................. 160 Section 18.6: Break specific nested loops............................................................................................................... 161 Section 18.7: "do... while" loop.................................................................................................................................. 161 Section 18.8: Break and continue labels................................................................................................................. 161 Chapter 19: Functions............................................................................................................................................. 163 Section 19.1: Function Scoping.................................................................................................................................. 163 Section 19.2: Currying................................................................................................................................................ 164 Section 19.3: Immediately Invoked Function Expressions..................................................................................... 165 Section 19.4: Named Functions................................................................................................................................ 166 Section 19.5: Binding `this` and arguments............................................................................................................. 169 Section 19.6: Functions with an Unknown Number of Arguments (variadic functions)..................................... 171 Section 19.7: Anonymous Function.......................................................................................................................... 172 Section 19.8: Default parameters............................................................................................................................ 174 Section 19.9: Call and apply...................................................................................................................................... 176 Section 19.10: Partial Application.............................................................................................................................. 177 Section 19.11: Passing arguments by reference or value....................................................................................... 178 Section 19.12: Function Arguments, "arguments" object, rest and spread parameters.................................... 179 Section 19.13: Function Composition........................................................................................................................ 179 Section 19.14: Get the name of a function object................................................................................................... 180 Section 19.15: Recursive Function............................................................................................................................. 180 Section 19.16: Using the Return Statement............................................................................................................. 181 Section 19.17: Functions as a variable..................................................................................................................... 182 Chapter 20: Functional JavaScript.................................................................................................................. 185 Section 20.1: Higher-Order Functions...................................................................................................................... 185 Section 20.2: Identity Monad................................................................................................................................... 185 Section 20.3: Pure Functions.................................................................................................................................... 187 Section 20.4: Accepting Functions as Arguments................................................................................................. 188 Chapter 21: Prototypes, objects........................................................................................................................ 190 Section 21.1: Creation and initialising Prototype..................................................................................................... 190 Chapter 22: Classes................................................................................................................................................. 192 Section 22.1: Class Constructor................................................................................................................................ 192 Section 22.2: Class Inheritance................................................................................................................................ 192 Section 22.3: Static Methods.................................................................................................................................... 193 Section 22.4: Getters and Setters............................................................................................................................ 193 Section 22.5: Private Members................................................................................................................................ 194 Section 22.6: Methods............................................................................................................................................... 195 Section 22.7: Dynamic Method Names................................................................................................................... 195 Section 22.8: Managing Private Data with Classes............................................................................................... 196 Section 22.9: Class Name binding........................................................................................................................... 198 Chapter 23: Namespacing.................................................................................................................................... 199 Section 23.1: Namespace by direct assignment.................................................................................................... 199 Section 23.2: Nested Namespaces.......................................................................................................................... 199 Chapter 24: Context (this).................................................................................................................................... 200 Section 24.1: this with simple objects....................................................................................................................... 200 Section 24.2: Saving this for use in nested functions / objects............................................................................ 200 Section 24.3: Binding function context.................................................................................................................... 201 Section 24.4: this in constructor functions.............................................................................................................. 202 Chapter 25: Setters and Getters...................................................................................................................... 203 Section 25.1: Defining a Setter/Getter Using Object.defineProperty................................................................... 203 Section 25.2: Defining an Setter/Getter in a Newly Created Object................................................................... 203 Section 25.3: Defining getters and setters in ES6 class......................................................................................... 203 Chapter 26: Events................................................................................................................................................... 205 Section 26.1: Page, DOM and Browser loading...................................................................................................... 205 Chapter 27: Inheritance......................................................................................................................................... 206 Section 27.1: Standard function prototype............................................................................................................. 206 Section 27.2: Dierence between Object.key and Object.prototype.key............................................................ 206 Section 27.3: Prototypal inheritance....................................................................................................................... 206 Section 27.4: Pseudo-classical inheritance............................................................................................................. 207 Section 27.5: Setting an Object's prototype........................................................................................................... 208 Chapter 28: Method Chaining............................................................................................................................. 210 Section 28.1: Chainable object design and chaining.............................................................................................. 210 Section 28.2: Method Chaining................................................................................................................................ 212 Chapter 29: Callbacks............................................................................................................................................. 213 Section 29.1: Simple Callback Usage Examples..................................................................................................... 213 Section 29.2: Continuation (synchronous and asynchronous)............................................................................ 214 Section 29.3: What is a callback?............................................................................................................................ 215 Section 29.4: Callbacks and `this`............................................................................................................................ 216 Section 29.5: Callback using Arrow function.......................................................................................................... 217 Section 29.6: Error handling and control-flow branching..................................................................................... 218 Chapter 30: Intervals and Timeouts............................................................................................................... 219 Section 30.1: Recursive setTimeout......................................................................................................................... 219 Section 30.2: Intervals............................................................................................................................................... 219 Section 30.3: Intervals............................................................................................................................................... 219 Section 30.4: Removing intervals............................................................................................................................ 220 Section 30.5: Removing timeouts............................................................................................................................ 220 Section 30.6: setTimeout, order of operations, clearTimeout.............................................................................. 220 Chapter 31: Regular expressions....................................................................................................................... 222 Section 31.1: Creating a RegExp Object................................................................................................................... 222 Section 31.2: RegExp Flags....................................................................................................................................... 222 Section 31.3: Check if string contains pattern using.test().................................................................................... 223 Section 31.4: Matching With.exec()......................................................................................................................... 223 Section 31.5: Using RegExp With Strings................................................................................................................. 223 Section 31.6: RegExp Groups.................................................................................................................................... 224 Section 31.7: Replacing string match with a callback function............................................................................. 225 Section 31.8: Using Regex.exec() with parentheses regex to extract matches of a string................................ 226 Chapter 32: Cookies................................................................................................................................................. 228 Section 32.1: Test if cookies are enabled................................................................................................................ 228 Section 32.2: Adding and Setting Cookies.............................................................................................................. 228 Section 32.3: Reading cookies................................................................................................................................. 228 Section 32.4: Removing cookies.............................................................................................................................. 228 Chapter 33: Web Storage..................................................................................................................................... 229 Section 33.1: Using localStorage.............................................................................................................................. 229 Section 33.2: Simpler way of handling Storage..................................................................................................... 229 Section 33.3: Storage events.................................................................................................................................... 230 Section 33.4: sessionStorage.................................................................................................................................... 231 Section 33.5: localStorage length............................................................................................................................ 232 Section 33.6: Error conditions................................................................................................................................... 232 Section 33.7: Clearing storage................................................................................................................................. 232 Section 33.8: Remove Storage Item........................................................................................................................ 232 Chapter 34: Data attributes............................................................................................................................... 233 Section 34.1: Accessing data attributes................................................................................................................... 233 Chapter 35: JSON..................................................................................................................................................... 234 Section 35.1: JSON versus JavaScript literals......................................................................................................... 234 Section 35.2: Parsing with a reviver function......................................................................................................... 235 Section 35.3: Serializing a value............................................................................................................................... 236 Section 35.4: Serializing and restoring class instances......................................................................................... 237 Section 35.5: Serializing with a replacer function.................................................................................................. 238 Section 35.6: Parsing a simple JSON string............................................................................................................ 239 Section 35.7: Cyclic object values............................................................................................................................ 239 Chapter 36: AJAX...................................................................................................................................................... 240 Section 36.1: Sending and Receiving JSON Data via POST.................................................................................. 240 Section 36.2: Add an AJAX preloader..................................................................................................................... 240 Section 36.3: Displaying the top JavaScript questions of the month from Stack Overflow's API.................... 241 Section 36.4: Using GET with parameters............................................................................................................... 242 Section 36.5: Check if a file exists via a HEAD request.......................................................................................... 243 Section 36.6: Using GET and no parameters.......................................................................................................... 243 Section 36.7: Listening to AJAX events at a global level....................................................................................... 243 Chapter 37: Enumerations................................................................................................................................... 244 Section 37.1: Enum definition using Object.freeze()............................................................................................... 244 Section 37.2: Alternate definition............................................................................................................................. 244 Section 37.3: Printing an enum variable................................................................................................................. 244 Section 37.4: Implementing Enums Using Symbols............................................................................................... 245 Section 37.5: Automatic Enumeration Value.......................................................................................................... 245 Chapter 38: Map........................................................................................................................................................ 247 Section 38.1: Creating a Map.................................................................................................................................... 247 Section 38.2: Clearing a Map................................................................................................................................... 247 Section 38.3: Removing an element from a Map.................................................................................................. 247 Section 38.4: Checking if a key exists in a Map...................................................................................................... 248 Section 38.5: Iterating Maps..................................................................................................................................... 248 Section 38.6: Getting and setting elements............................................................................................................ 248 Section 38.7: Getting the number of elements of a Map...................................................................................... 249 Chapter 39: Timestamps....................................................................................................................................... 250 Section 39.1: High-resolution timestamps............................................................................................................... 250 Section 39.2: Get Timestamp in Seconds................................................................................................................ 250 Section 39.3: Low-resolution timestamps............................................................................................................... 250 Section 39.4: Support for legacy browsers............................................................................................................. 250 Chapter 40: Unary Operators........................................................................................................................... 251 Section 40.1: Overview.............................................................................................................................................. 251 Section 40.2: The typeof operator.......................................................................................................................... 251 Section 40.3: The delete operator........................................................................................................................... 252 Section 40.4: The unary plus operator (+).............................................................................................................. 253 Section 40.5: The void operator.............................................................................................................................. 254 Section 40.6: The unary negation operator (-)...................................................................................................... 255 Section 40.7: The bitwise NOT operator (~)............................................................................................................ 255 Section 40.8: The logical NOT operator (!)............................................................................................................. 256 Chapter 41: Generators......................................................................................................................................... 258 Section 41.1: Generator Functions............................................................................................................................ 258 Section 41.2: Sending Values to Generator............................................................................................................. 259 Section 41.3: Delegating to other Generator.......................................................................................................... 259 Section 41.4: Iteration................................................................................................................................................ 259 Section 41.5: Async flow with generators................................................................................................................ 260 Section 41.6: Iterator-Observer interface................................................................................................................ 261 Chapter 42: Promises............................................................................................................................................. 263 Section 42.1: Introduction.......................................................................................................................................... 263 Section 42.2: Promise chaining................................................................................................................................ 264 Section 42.3: Waiting for multiple concurrent promises....................................................................................... 265 Section 42.4: Reduce an array to chained promises............................................................................................ 266 Section 42.5: Waiting for the first of multiple concurrent promises.................................................................... 267 Section 42.6: "Promisifying" functions with callbacks........................................................................................... 268 Section 42.7: Error Handling.................................................................................................................................... 268 Section 42.8: Reconciling synchronous and asynchronous operations.............................................................. 272 Section 42.9: Delay function call.............................................................................................................................. 273 Section 42.10: "Promisifying" values........................................................................................................................ 273 Section 42.11: Using ES2017 async/await................................................................................................................ 274 Section 42.12: Performing cleanup with finally().................................................................................................... 274 Section 42.13: forEach with promises...................................................................................................................... 275 Section 42.14: Asynchronous API request............................................................................................................... 275 Chapter 43: Set.......................................................................................................................................................... 277 Section 43.1: Creating a Set...................................................................................................................................... 277 Section 43.2: Adding a value to a Set..................................................................................................................... 277 Section 43.3: Removing value from a set............................................................................................................... 277 Section 43.4: Checking if a value exist in a set....................................................................................................... 278 Section 43.5: Clearing a Set...................................................................................................................................... 278 Section 43.6: Getting set length............................................................................................................................... 278 Section 43.7: Converting Sets to arrays.................................................................................................................. 278 Section 43.8: Intersection and dierence in Sets................................................................................................... 279 Section 43.9: Iterating Sets....................................................................................................................................... 279 Chapter 44: Modals - Prompts.......................................................................................................................... 280 Section 44.1: About User Prompts........................................................................................................................... 280 Section 44.2: Persistent Prompt Modal................................................................................................................... 280 Section 44.3: Confirm to Delete element................................................................................................................ 281 Section 44.4: Usage of alert().................................................................................................................................. 281 Section 44.5: Usage of prompt()............................................................................................................................. 282 Chapter 45: execCommand and contenteditable................................................................................... 283 Section 45.1: Listening to Changes of contenteditable.......................................................................................... 284 Section 45.2: Getting started.................................................................................................................................... 284 Section 45.3: Copy to clipboard from textarea using execCommand("copy").................................................. 285 Section 45.4: Formatting.......................................................................................................................................... 285 Chapter 46: History................................................................................................................................................. 287 Section 46.1: history.pushState().............................................................................................................................. 287 Section 46.2: history.replaceState()........................................................................................................................ 287 Section 46.3: Load a specific URL from the history list......................................................................................... 287 Chapter 47: Navigator Object........................................................................................................................... 289 Section 47.1: Get some basic browser data and return it as a JSON object...................................................... 289 Chapter 48: BOM (Browser Object Model).................................................................................................. 290 Section 48.1: Introduction.......................................................................................................................................... 290 Section 48.2: Window Object Properties................................................................................................................. 290 Section 48.3: Window Object Methods.................................................................................................................... 291 Chapter 49: The Event Loop............................................................................................................................... 292 Section 49.1: The event loop in a web browser...................................................................................................... 292 Section 49.2: Asynchronous operations and the event loop................................................................................ 293 Chapter 50: Strict mode....................................................................................................................................... 294 Section 50.1: For entire scripts................................................................................................................................. 294 Section 50.2: For functions....................................................................................................................................... 294 Section 50.3: Changes to properties....................................................................................................................... 294 Section 50.4: Changes to global properties........................................................................................................... 295 Section 50.5: Duplicate Parameters........................................................................................................................ 296 Section 50.6: Function scoping in strict mode........................................................................................................ 296 Section 50.7: Behaviour of a function's arguments list......................................................................................... 296 Section 50.8: Non-Simple parameter lists.............................................................................................................. 297 Chapter 51: Custom Elements............................................................................................................................ 299 Section 51.1: Extending Native Elements................................................................................................................. 299 Section 51.2: Registering New Elements................................................................................................................. 299 Chapter 52: Data Manipulation......................................................................................................................... 300 Section 52.1: Format numbers as money............................................................................................................... 300 Section 52.2: Extract extension from file name..................................................................................................... 300 Section 52.3: Set object property given its string name....................................................................................... 301 Chapter 53: Binary Data....................................................................................................................................... 302 Section 53.1: Getting binary representation of an image file............................................................................... 302 Section 53.2: Converting between Blobs and ArrayBuers................................................................................. 302 Section 53.3: Manipulating ArrayBuers with DataViews..................................................................................... 303 Section 53.4: Creating a TypedArray from a Base64 string................................................................................. 303 Section 53.5: Using TypedArrays............................................................................................................................. 304 Section 53.6: Iterating through an arrayBuer...................................................................................................... 304 Chapter 54: Template Literals.......................................................................................................................... 306 Section 54.1: Basic interpolation and multiline strings........................................................................................... 306 Section 54.2: Tagged strings................................................................................................................................... 306 Section 54.3: Raw strings.......................................................................................................................................... 307 Section 54.4: Templating HTML With Template Strings....................................................................................... 307 Section 54.5: Introduction......................................................................................................................................... 308 Chapter 55: Fetch..................................................................................................................................................... 309 Section 55.1: Getting JSON data.............................................................................................................................. 309 Section 55.2: Set Request Headers......................................................................................................................... 309 Section 55.3: POST Data........................................................................................................................................... 309 Section 55.4: Send cookies....................................................................................................................................... 310 Section 55.5: GlobalFetch......................................................................................................................................... 310 Section 55.6: Using Fetch to Display Questions from the Stack Overflow API................................................... 310 Chapter 56: Scope.................................................................................................................................................... 311 Section 56.1: Closures................................................................................................................................................ 311 Section 56.2: Hoisting................................................................................................................................................ 312 Section 56.3: Dierence between var and let........................................................................................................ 315 Section 56.4: Apply and Call syntax and invocation............................................................................................. 317 Section 56.5: Arrow function invocation................................................................................................................. 318 Section 56.6: Bound invocation................................................................................................................................ 319 Section 56.7: Method invocation.............................................................................................................................. 319 Section 56.8: Anonymous invocation...................................................................................................................... 320 Section 56.9: Constructor invocation...................................................................................................................... 320 Section 56.10: Using let in loops instead of var (click handlers example)........................................................... 320 Chapter 57: Modules............................................................................................................................................... 322 Section 57.1: Defining a module............................................................................................................................... 322 Section 57.2: Default exports................................................................................................................................... 322 Section 57.3: Importing named members from another module........................................................................ 323 Section 57.4: Importing an entire module.............................................................................................................. 323 Section 57.5: Importing named members with aliases......................................................................................... 324 Section 57.6: Importing with side eects................................................................................................................ 324 Section 57.7: Exporting multiple named members................................................................................................ 324 Chapter 58: Screen.................................................................................................................................................. 325 Section 58.1: Getting the screen resolution............................................................................................................. 325 Section 58.2: Getting the “available” area of the screen...................................................................................... 325 Section 58.3: Page width and height....................................................................................................................... 325 Section 58.4: Window innerWidth and innerHeight Properties............................................................................ 325 Section 58.5: Getting color information about the screen.................................................................................... 325 Chapter 59: Variable coercion/conversion................................................................................................. 326 Section 59.1: Double Negation (!!x).......................................................................................................................... 326 Section 59.2: Implicit conversion............................................................................................................................. 326 Section 59.3: Converting to boolean....................................................................................................................... 326 Section 59.4: Converting a string to a number...................................................................................................... 327 Section 59.5: Converting a number to a string...................................................................................................... 328 Section 59.6: Primitive to Primitive conversion table............................................................................................. 328 Section 59.7: Convert an array to a string............................................................................................................. 328 Section 59.8: Array to String using array methods............................................................................................... 329 Section 59.9: Converting a number to a boolean.................................................................................................. 329 Section 59.10: Converting a string to a boolean.................................................................................................... 329 Section 59.11: Integer to Float................................................................................................................................... 329 Section 59.12: Float to Integer.................................................................................................................................. 330 Section 59.13: Convert string to float....................................................................................................................... 330 Chapter 60: Destructuring assignment......................................................................................................... 331 Section 60.1: Destructuring Objects......................................................................................................................... 331 Section 60.2: Destructuring function arguments................................................................................................... 332 Section 60.3: Nested Destructuring......................................................................................................................... 332 Section 60.4: Destructuring Arrays.......................................................................................................................... 333 Section 60.5: Destructuring inside variables.......................................................................................................... 333 Section 60.6: Default Value While Destructuring................................................................................................... 334 Section 60.7: Renaming Variables While Destructuring........................................................................................ 334 Chapter 61: WebSockets....................................................................................................................................... 335 Section 61.1: Working with string messages........................................................................................................... 335 Section 61.2: Establish a web socket connection................................................................................................... 335 Section 61.3: Working with binary messages......................................................................................................... 335 Section 61.4: Making a secure web socket connection......................................................................................... 336 Chapter 62: Arrow Functions.............................................................................................................................. 337 Section 62.1: Introduction.......................................................................................................................................... 337 Section 62.2: Lexical Scoping & Binding (Value of "this")..................................................................................... 337 Section 62.3: Arguments Object.............................................................................................................................. 338 Section 62.4: Implicit Return..................................................................................................................................... 338 Section 62.5: Arrow functions as a constructor..................................................................................................... 339 Section 62.6: Explicit Return..................................................................................................................................... 339 Chapter 63: Workers............................................................................................................................................... 340 Section 63.1: Web Worker......................................................................................................................................... 340 Section 63.2: A simple service worker..................................................................................................................... 340 Section 63.3: Register a service worker.................................................................................................................. 341 Section 63.4: Communicating with a Web Worker................................................................................................ 341 Section 63.5: Terminate a worker............................................................................................................................ 342 Section 63.6: Populating your cache....................................................................................................................... 343 Section 63.7: Dedicated Workers and Shared Workers........................................................................................ 343 Chapter 64: requestAnimationFrame............................................................................................................ 345 Section 64.1: Use requestAnimationFrame to fade in element............................................................................ 345 Section 64.2: Keeping Compatibility....................................................................................................................... 346 Section 64.3: Cancelling an Animation.................................................................................................................... 346 Chapter 65: Creational Design Patterns....................................................................................................... 348 Section 65.1: Factory Functions................................................................................................................................ 348 Section 65.2: Factory with Composition.................................................................................................................. 349 Section 65.3: Module and Revealing Module Patterns.......................................................................................... 350 Section 65.4: Prototype Pattern............................................................................................................................... 352 Section 65.5: Singleton Pattern................................................................................................................................ 353 Section 65.6: Abstract Factory Pattern................................................................................................................... 354 Chapter 66: Detecting browser......................................................................................................................... 355 Section 66.1: Feature Detection Method................................................................................................................. 355 Section 66.2: User Agent Detection......................................................................................................................... 355 Section 66.3: Library Method................................................................................................................................... 356 Chapter 67: Symbols............................................................................................................................................... 357 Section 67.1: Basics of symbol primitive type......................................................................................................... 357 Section 67.2: Using Symbol.for() to create global, shared symbols.................................................................... 357 Section 67.3: Converting a symbol into a string.................................................................................................... 357 Chapter 68: Transpiling......................................................................................................................................... 359 Section 68.1: Introduction to Transpiling................................................................................................................. 359 Section 68.2: Start using ES6/7 with Babel............................................................................................................. 360 Chapter 69: Automatic Semicolon Insertion - ASI.................................................................................... 361 Section 69.1: Avoid semicolon insertion on return statements............................................................................. 361 Section 69.2: Rules of Automatic Semicolon Insertion.......................................................................................... 361 Section 69.3: Statements aected by automatic semicolon insertion................................................................ 362 Chapter 70: Localization....................................................................................................................................... 364 Section 70.1: Number formatting............................................................................................................................. 364 Section 70.2: Currency formatting.......................................................................................................................... 364 Section 70.3: Date and time formatting.................................................................................................................. 364 Chapter 71: Geolocation........................................................................................................................................ 365 Section 71.1: Get updates when a user's location changes................................................................................... 365 Section 71.2: Get a user's latitude and longitude................................................................................................... 365 Section 71.3: More descriptive error codes............................................................................................................. 365 Chapter 72: IndexedDB.......................................................................................................................................... 367 Section 72.1: Opening a database........................................................................................................................... 367 Section 72.2: Adding objects.................................................................................................................................... 367 Section 72.3: Retrieving data................................................................................................................................... 368 Section 72.4: Testing for IndexedDB availability.................................................................................................... 369 Chapter 73: Modularization Techniques....................................................................................................... 370 Section 73.1: ES6 Modules......................................................................................................................................... 370 Section 73.2: Universal Module Definition (UMD).................................................................................................. 370 Section 73.3: Immediately invoked function expressions (IIFE)............................................................................ 371 Section 73.4: Asynchronous Module Definition (AMD).......................................................................................... 371 Section 73.5: CommonJS - Node.js.......................................................................................................................... 372 Chapter 74: Proxy.................................................................................................................................................... 374 Section 74.1: Proxying property lookup.................................................................................................................. 374 Section 74.2: Very simple proxy (using the set trap)............................................................................................ 374 Chapter 75:.postMessage() and MessageEvent...................................................................................... 376 Section 75.1: Getting Started.................................................................................................................................... 376 Chapter 76: WeakMap............................................................................................................................................ 379 Section 76.1: Creating a WeakMap object.............................................................................................................. 379 Section 76.2: Getting a value associated to the key.............................................................................................. 379 Section 76.3: Assigning a value to the key............................................................................................................. 379 Section 76.4: Checking if an element with the key exists...................................................................................... 379 Section 76.5: Removing an element with the key.................................................................................................. 380 Section 76.6: Weak reference demo....................................................................................................................... 380 Chapter 77: WeakSet.............................................................................................................................................. 382 Section 77.1: Creating a WeakSet object................................................................................................................ 382 Section 77.2: Adding a value.................................................................................................................................... 382 Section 77.3: Checking if a value exists................................................................................................................... 382 Section 77.4: Removing a value............................................................................................................................... 382 Chapter 78: Escape Sequences......................................................................................................................... 383 Section 78.1: Entering special characters in strings and regular expressions.................................................... 383 Section 78.2: Escape sequence types..................................................................................................................... 383 Chapter 79: Behavioral Design Patterns...................................................................................................... 386 Section 79.1: Observer pattern................................................................................................................................. 386 Section 79.2: Mediator Pattern................................................................................................................................ 387 Section 79.3: Command............................................................................................................................................ 388 Section 79.4: Iterator................................................................................................................................................. 389 Chapter 80: Server-sent events........................................................................................................................ 391 Section 80.1: Setting up a basic event stream to the server................................................................................. 391 Section 80.2: Closing an event stream................................................................................................................... 391 Section 80.3: Binding event listeners to EventSource........................................................................................... 391 Chapter 81: Async functions (async/await)................................................................................................ 393 Section 81.1: Introduction.......................................................................................................................................... 393 Section 81.2: Await and operator precedence....................................................................................................... 393 Section 81.3: Async functions compared to Promises........................................................................................... 394 Section 81.4: Looping with async await................................................................................................................... 395 Section 81.5: Less indentation.................................................................................................................................. 396 Section 81.6: Simultaneous async (parallel) operations........................................................................................ 397 Chapter 82: Async Iterators................................................................................................................................ 398 Section 82.1: Basics................................................................................................................................................... 398 Chapter 83: How to make iterator usable inside async callback function................................. 399 Section 83.1: Erroneous code, can you spot why this usage of key will lead to bugs?...................................... 399 Section 83.2: Correct Writing................................................................................................................................... 399 Chapter 84: Tail Call Optimization.................................................................................................................. 400 Section 84.1: What is Tail Call Optimization (TCO)................................................................................................ 400 Section 84.2: Recursive loops.................................................................................................................................. 400 Chapter 85: Bitwise Operators - Real World Examples (snippets)................................................. 401 Section 85.1: Swapping Two Integers with Bitwise XOR (without additional memory allocation).................... 401 Section 85.2: Faster multiplication or division by powers of 2............................................................................. 401 Section 85.3: Number's Parity Detection with Bitwise AND.................................................................................. 401 Chapter 86: Tilde ~................................................................................................................................................... 403 Section 86.1: ~ Integer............................................................................................................................................... 403 Section 86.2: ~~ Operator......................................................................................................................................... 403 Section 86.3: Converting Non-numeric values to Numbers.................................................................................. 404 Section 86.4: Shorthands.......................................................................................................................................... 404 Section 86.5: ~ Decimal............................................................................................................................................. 404 Chapter 87: Using JavaScript to get/set CSS custom variables..................................................... 406 Section 87.1: How to get and set CSS variable property values........................................................................... 406 Chapter 88: Selection API..................................................................................................................................... 407 Section 88.1: Get the text of the selection............................................................................................................... 407 Section 88.2: Deselect everything that is selected................................................................................................ 407 Section 88.3: Select the contents of an element.................................................................................................... 407 Chapter 89: File API, Blobs and FileReaders............................................................................................... 408 Section 89.1: Read file as string............................................................................................................................... 408 Section 89.2: Read file as dataURL......................................................................................................................... 408 Section 89.3: Slice a file............................................................................................................................................. 409 Section 89.4: Get the properties of the file............................................................................................................. 409 Section 89.5: Selecting multiple files and restricting file types............................................................................. 410 Section 89.6: Client side csv download using Blob................................................................................................ 410 Chapter 90: Notifications API............................................................................................................................. 411 Section 90.1