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: Datatypes in JavaScript ................................................................................................................. 17
Section 4.1: typeof ....................................................................................................................................................... 17
Section 4.2: Finding an object's class ........................................................................................................................ 18
Section 4.3: Getting object type by constructor name ............................................................................................ 18
Chapter 5: Arithmetic (Math) ................................................................................................................................ 21
Section 5.1: Constants ................................................................................................................................................. 21
Section 5.2: Remainder / Modulus (%) ..................................................................................................................... 21
Section 5.3: Rounding ................................................................................................................................................. 22
Section 5.4: Trigonometry .......................................................................................................................................... 24
Section 5.5: Bitwise operators .................................................................................................................................... 25
Section 5.6: Incrementing (++) ................................................................................................................................... 27
Section 5.7: Exponentiation (Math.pow() or **) ......................................................................................................... 27
Section 5.8: Random Integers and Floats ................................................................................................................. 28
Section 5.9: Addition (+) .............................................................................................................................................. 28
Section 5.10: Little / Big endian for typed arrays when using bitwise operators ................................................. 29
Section 5.11: Get Random Between Two Numbers .................................................................................................. 30
Section 5.12: Simulating events with dierent probabilities .................................................................................... 31
Section 5.13: Subtraction (-) ........................................................................................................................................ 32
Section 5.14: Multiplication (*) ..................................................................................................................................... 32
Section 5.15: Getting maximum and minimum ......................................................................................................... 32
Section 5.16: Restrict Number to Min/Max Range ................................................................................................... 33
Section 5.17: Ceiling and Floor .................................................................................................................................... 33
Section 5.18: Getting roots of a number .................................................................................................................... 34
Section 5.19: Random with gaussian distribution ..................................................................................................... 34
Section 5.20: Math.atan2 to find direction ................................................................................................................ 35
, Section 5.21: Sin & Cos to create a vector given direction & distance ................................................................... 35
Section 5.22: Math.hypot ............................................................................................................................................ 36
Section 5.23: Periodic functions using Math.sin ........................................................................................................ 36
Section 5.24: Division (/) ............................................................................................................................................. 38
Section 5.25: Decrementing (--) ................................................................................................................................ 38
Chapter 6: Bitwise operators ................................................................................................................................ 40
Section 6.1: Bitwise operators ..................................................................................................................................... 40
Section 6.2: Shift Operators ........................................................................................................................................ 42
Chapter 7: Strings ....................................................................................................................................................... 43
Section 7.1: Basic Info and String Concatenation ..................................................................................................... 43
Section 7.2: Reverse String ......................................................................................................................................... 44
Section 7.3: Comparing Strings Lexicographically ................................................................................................... 45
Section 7.4: Access character at index in string ....................................................................................................... 46
Section 7.5: Escaping quotes ...................................................................................................................................... 46
Section 7.6: Word Counter .......................................................................................................................................... 47
Section 7.7: Trim whitespace ...................................................................................................................................... 47
Section 7.8: Splitting a string into an array .............................................................................................................. 47
Section 7.9: Strings are unicode ................................................................................................................................. 48
Section 7.10: Detecting a string .................................................................................................................................. 48
Section 7.11: Substrings with slice ............................................................................................................................... 49
Section 7.12: Character code ...................................................................................................................................... 49
Section 7.13: String Representations of Numbers .................................................................................................... 49
Section 7.14: String Find and Replace Functions ...................................................................................................... 50
Section 7.15: Find the index of a substring inside a string ....................................................................................... 51
Section 7.16: String to Upper Case ............................................................................................................................. 51
Section 7.17: String to Lower Case ............................................................................................................................. 52
Section 7.18: Repeat a String ...................................................................................................................................... 52
Chapter 8: Date ........................................................................................................................................................... 53
Section 8.1: Create a new Date object ....................................................................................................................... 53
Section 8.2: Convert to a string format .................................................................................................................... 55
Section 8.3: Creating a Date from UTC ..................................................................................................................... 56
Section 8.4: Formatting a JavaScript date ............................................................................................................... 59
Section 8.5: Get the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC ................................. 61
Section 8.6: Get the current time and date ............................................................................................................... 61
Section 8.7: Increment a Date Object ....................................................................................................................... 62
Section 8.8: Convert to JSON ..................................................................................................................................... 63
Chapter 9: Date Comparison ................................................................................................................................ 64
Section 9.1: Comparing Date values .......................................................................................................................... 64
Section 9.2: Date Dierence Calculation .................................................................................................................. 65
Chapter 10: Comparison Operations ................................................................................................................ 66
Section 10.1: Abstract equality / inequality and type conversion ........................................................................... 66
Section 10.2: NaN Property of the Global Object ..................................................................................................... 67
Section 10.3: Short-circuiting in boolean operators ................................................................................................. 69
Section 10.4: Null and Undefined ............................................................................................................................... 71
Section 10.5: Abstract Equality (==) ........................................................................................................................... 71
Section 10.6: Logic Operators with Booleans ........................................................................................................... 72
Section 10.7: Automatic Type Conversions ............................................................................................................... 73
Section 10.8: Logic Operators with Non-boolean values (boolean coercion) ....................................................... 73
Section 10.9: Empty Array ........................................................................................................................................... 74
Section 10.10: Equality comparison operations ........................................................................................................ 74
, Section 10.11: Relational operators (<, <=, >, >=) ........................................................................................................ 76
Section 10.12: Inequality .............................................................................................................................................. 77
Section 10.13: List of Comparison Operators ............................................................................................................ 78
Section 10.14: Grouping multiple logic statements ................................................................................................... 78
Section 10.15: Bit fields to optimise comparison of multi state data ...................................................................... 78
Chapter 11: Constructor functions ...................................................................................................................... 80
Section 11.1: Declaring a constructor function .......................................................................................................... 80
Chapter 12: Conditions .............................................................................................................................................. 81
Section 12.1: Ternary operators .................................................................................................................................. 81
Section 12.2: Switch statement ................................................................................................................................... 82
Section 12.3: If / Else If / Else Control ........................................................................................................................ 84
Section 12.4: Strategy .................................................................................................................................................. 85
Section 12.5: Using || and && short circuiting ............................................................................................................ 86
Chapter 13: Comments ............................................................................................................................................. 87
Section 13.1: Using Comments .................................................................................................................................... 87
Section 13.2: Using HTML comments in JavaScript (Bad practice) ....................................................................... 87
Chapter 14: Arrays ..................................................................................................................................................... 89
Section 14.1: Converting Array-like Objects to Arrays ............................................................................................. 89
Section 14.2: Reducing values .................................................................................................................................... 91
Section 14.3: Mapping values ..................................................................................................................................... 93
Section 14.4: Filtering Object Arrays .......................................................................................................................... 93
Section 14.5: Sorting Arrays ........................................................................................................................................ 95
Section 14.6: Iteration .................................................................................................................................................. 97
Section 14.7: Destructuring an array ....................................................................................................................... 101
Section 14.8: Removing duplicate elements ........................................................................................................... 102
Section 14.9: Array comparison ............................................................................................................................... 102
Section 14.10: Reversing arrays ................................................................................................................................ 103
Section 14.11: Shallow cloning an array ................................................................................................................... 104
Section 14.12: Concatenating Arrays ....................................................................................................................... 104
Section 14.13: Merge two array as key value pair .................................................................................................. 106
Section 14.14: Array spread / rest ............................................................................................................................ 106
Section 14.15: Filtering values ................................................................................................................................... 107
Section 14.16: Searching an Array ............................................................................................................................ 108
Section 14.17: Convert a String to an Array ............................................................................................................ 109
Section 14.18: Removing items from an array ........................................................................................................ 109
Section 14.19: Removing all elements ...................................................................................................................... 110
Section 14.20: Finding the minimum or maximum element .................................................................................. 111
Section 14.21: Standard array initialization ............................................................................................................. 112
Section 14.22: Joining array elements in a string .................................................................................................. 113
Section 14.23: Removing/Adding elements using splice() .................................................................................... 114
Section 14.24: The entries() method ........................................................................................................................ 114
Section 14.25: Remove value from array ................................................................................................................ 114
Section 14.26: Flattening Arrays ............................................................................................................................... 115
Section 14.27: Append / Prepend items to Array ................................................................................................... 116
Section 14.28: Object keys and values to array ..................................................................................................... 116
Section 14.29: Logical connective of values ........................................................................................................... 117
Section 14.30: Checking if an object is an Array .................................................................................................... 117
Section 14.31: Insert an item into an array at a specific index .............................................................................. 118
Section 14.32: Sorting multidimensional array ....................................................................................................... 118
Section 14.33: Test all array items for equality ...................................................................................................... 119
, Section 14.34: Copy part of an Array ...................................................................................................................... 119
Chapter 15: Objects .................................................................................................................................................. 121
Section 15.1: Shallow cloning .................................................................................................................................... 121
Section 15.2: Object.freeze ........................................................................................................................................ 121
Section 15.3: Object cloning ...................................................................................................................................... 122
Section 15.4: Object properties iteration ................................................................................................................. 123
Section 15.5: Object.assign ....................................................................................................................................... 124
Section 15.6: Object rest/spread (...) ........................................................................................................................ 125
Section 15.7: Object.defineProperty ......................................................................................................................... 125
Section 15.8: Accesor properties (get and set) ....................................................................................................... 126
Section 15.9: Dynamic / variable property names ................................................................................................ 126
Section 15.10: Arrays are Objects ............................................................................................................................. 127
Section 15.11: Object.seal ........................................................................................................................................... 128
Section 15.12: Convert object's values to array ...................................................................................................... 129
Section 15.13: Retrieving properties from an object .............................................................................................. 129
Section 15.14: Read-Only property .......................................................................................................................... 132
Section 15.15: Non enumerable property ................................................................................................................ 132
Section 15.16: Lock property description ................................................................................................................. 132
Section 15.17: Object.getOwnPropertyDescriptor ................................................................................................... 133
Section 15.18: Descriptors and Named Properties ................................................................................................. 133
Section 15.19: Object.keys ......................................................................................................................................... 135
Section 15.20: Properties with special characters or reserved words ................................................................. 135
Section 15.21: Creating an Iterable object ............................................................................................................... 136
Section 15.22: Iterating over Object entries - Object.entries() .............................................................................. 136
Section 15.23: Object.values() ................................................................................................................................... 137
Chapter 16: Declarations and Assignments ................................................................................................ 138
Section 16.1: Modifying constants ............................................................................................................................ 138
Section 16.2: Declaring and initializing constants .................................................................................................. 138
Section 16.3: Declaration ........................................................................................................................................... 138
Section 16.4: Undefined ............................................................................................................................................. 139
Section 16.5: Data Types ........................................................................................................................................... 139
Section 16.6: Mathematic operations and assignment .......................................................................................... 139
Section 16.7: Assignment .......................................................................................................................................... 141
Chapter 17: Loops ..................................................................................................................................................... 142
Section 17.1: Standard "for" loops ............................................................................................................................ 142
Section 17.2: "for ... of" loop ...................................................................................................................................... 143
Section 17.3: "for ... in" loop ....................................................................................................................................... 145
Section 17.4: "while" Loops ....................................................................................................................................... 145
Section 17.5: "continue" a loop ................................................................................................................................. 146
Section 17.6: Break specific nested loops ............................................................................................................... 147
Section 17.7: "do ... while" loop .................................................................................................................................. 147
Section 17.8: Break and continue labels .................................................................................................................. 147
Chapter 18: Functions ............................................................................................................................................. 149
Section 18.1: Function Scoping .................................................................................................................................. 149
Section 18.2: Currying ............................................................................................................................................... 150
Section 18.3: Immediately Invoked Function Expressions ..................................................................................... 151
Section 18.4: Named Functions ................................................................................................................................ 152
Section 18.5: Binding `this` and arguments ............................................................................................................. 155
Section 18.6: Functions with an Unknown Number of Arguments (variadic functions) ..................................... 157
Section 18.7: Anonymous Function .......................................................................................................................... 158