Javascript for Impatient Programmers (ES2021 Edition)
Total Page:16
File Type:pdf, Size:1020Kb
2 JavaScript for impatient programmers (ES2021 edition) Dr. Axel Rauschmayer 2021 “An exhaustive resource, yet cuts out the fluff that clutters many programming books – with explanations that are understandable and to the point, as promised by the title! The quizzes and exercises are a very useful feature to check and lock in your knowledge. And you can definitely tear through the book fairly quickly, to get up and running in JavaScript.” — Pam Selle, thewebivore.com “The best introductory book for modern JavaScript.” — Tejinder Singh, Senior Software Engineer, IBM “This is JavaScript. No filler. No frameworks. No third-party libraries. If you want to learn JavaScript, you need this book.” — Shelley Powers, Software Engineer/Writer Copyright © 2021 by Dr. Axel Rauschmayer Cover by Fran Caye All rights reserved. This book or any portion thereof may not be reproduced or used in any manner whatsoever without the express written permission of the publisher except for the use of brief quotations in a book review or scholarly journal. ISBN 978-1-09-121009-7 exploringjs.com Contents I Background 9 1 Beforeyoubuythebook 11 1.1 About the content ............................... 11 1.2 Previewing and buying this book ...................... 12 1.3 About the author ............................... 12 1.4 Acknowledgements .............................. 13 2 FAQ: book and supplementary material 15 2.1 Howtoreadthisbook ............................ 15 2.2 I own a digital version ............................ 16 2.3 I own the print version ............................ 17 2.4 Notations and conventions .......................... 17 3 History and evolution of JavaScript 19 3.1 How JavaScript was created ......................... 19 3.2 Standardizing JavaScript ........................... 20 3.3 Timeline of ECMAScript versions ...................... 20 3.4 Ecma Technical Committee 39 (TC39) .................... 21 3.5 The TC39 process ............................... 21 3.6 FAQ: TC39 process .............................. 23 3.7 Evolving JavaScript: Don’t break the web .................. 23 4 New JavaScript features 25 4.1 New in ECMAScript 2021 .......................... 25 4.2 New in ECMAScript 2020 .......................... 25 4.3 New in ECMAScript 2019 .......................... 26 4.4 New in ECMAScript 2018 .......................... 26 4.5 New in ECMAScript 2017 .......................... 26 4.6 New in ECMAScript 2016 .......................... 27 4.7 Source of this chapter ............................. 27 5 FAQ: JavaScript 29 5.1 What are good references for JavaScript? .................. 29 5.2 How do I find out what JavaScript features are supported where? .... 29 5.3 Where can I look up what features are planned for JavaScript? ...... 30 5.4 Why does JavaScript fail silently so often? ................. 30 3 4 CONTENTS 5.5 Why can’t we clean up JavaScript, by removing quirks and outdated fea- tures? ..................................... 30 5.6 How can I quickly try out a piece of JavaScript code? ........... 30 II Firststeps 31 6 Using JavaScript: the big picture 33 6.1 What are you learning in this book? ..................... 33 6.2 The structure of browsers and Node.js ................... 33 6.3 JavaScript references ............................. 34 6.4 Further reading ................................ 34 7 Syntax 35 7.1 An overview of JavaScript’s syntax ..................... 36 7.2 (Advanced) .................................. 43 7.3 Identifiers ................................... 43 7.4 Statement vs. expression ........................... 44 7.5 Ambiguous syntax .............................. 46 7.6 Semicolons .................................. 47 7.7 Automatic semicolon insertion (ASI) .................... 48 7.8 Semicolons: best practices .......................... 49 7.9 Strict mode vs. sloppy mode ......................... 50 8 Consoles: interactive JavaScript command lines 53 8.1 Trying out JavaScript code .......................... 53 8.2 The console.* API: printing data and more ................ 55 9 Assertion API 59 9.1 Assertions in software development ..................... 59 9.2 How assertions are used in this book .................... 59 9.3 Normal comparison vs. deep comparison .................. 60 9.4 Quick reference: module assert ....................... 61 10 Getting started with quizzes and exercises 65 10.1 Quizzes .................................... 65 10.2 Exercises .................................... 65 10.3 Unit tests in JavaScript ............................ 66 III Variables and values 71 11 Variables and assignment 73 11.1 let ....................................... 74 11.2 const ..................................... 74 11.3 Deciding between const and let ...................... 75 11.4 The scope of a variable ............................ 75 11.5 (Advanced) .................................. 77 11.6 Terminology: static vs. dynamic ....................... 77 11.7 Global variables and the global object .................... 78 CONTENTS 5 11.8 Declarations: scope and activation ...................... 80 11.9 Closures .................................... 84 12 Values 87 12.1 What’s a type? ................................. 87 12.2 JavaScript’s type hierarchy .......................... 88 12.3 The types of the language specification ................... 88 12.4 Primitive values vs. objects .......................... 89 12.5 The operators typeof and instanceof: what’s the type of a value? .... 91 12.6 Classes and constructor functions ...................... 93 12.7 Converting between types .......................... 94 13 Operators 97 13.1 Making sense of operators .......................... 97 13.2 The plus operator (+) ............................. 98 13.3 Assignment operators ............................ 99 13.4 Equality: == vs. === .............................. 100 13.5 Ordering operators .............................. 103 13.6 Various other operators ............................ 104 IV Primitive values 105 14 The non-values undefined and null 107 14.1 undefined vs. null .............................. 107 14.2 Occurrences of undefined and null ..................... 108 14.3 Checking for undefined or null ....................... 109 14.4 The nullish coalescing operator (??) for default values [ES2020] ...... 109 14.5 undefined and null don’t have properties ................. 112 14.6 The history of undefined and null ..................... 113 15 Booleans 115 15.1 Converting to boolean ............................ 115 15.2 Falsy and truthy values ............................ 116 15.3 Truthiness-based existence checks ...................... 117 15.4 Conditional operator (?:) .......................... 119 15.5 Binary logical operators: And (x && y), Or (x || y) ............ 120 15.6 Logical Not (!) ................................ 122 16 Numbers 123 16.1 Numbers are used for both floating point numbers and integers ..... 124 16.2 Number literals ................................ 124 16.3 Arithmetic operators ............................. 126 16.4 Converting to number ............................ 129 16.5 Error values .................................. 130 16.6 The precision of numbers: careful with decimal fractions ......... 132 16.7 (Advanced) .................................. 132 16.8 Background: floating point precision .................... 132 16.9 Integer numbers in JavaScript ........................ 134 6 CONTENTS 16.10Bitwise operators ............................... 137 16.11Quick reference: numbers .......................... 139 17 Math 145 17.1 Data properties ................................ 145 17.2 Exponents, roots, logarithms ......................... 146 17.3 Rounding ................................... 147 17.4 Trigonometric Functions ........................... 148 17.5 Various other functions ............................ 150 17.6 Sources .................................... 151 18 Bigints – arbitrary-precision integers [ES2020] (advanced) 153 18.1 Why bigints? ................................. 154 18.2 Bigints ..................................... 154 18.3 Bigint literals ................................. 156 18.4 Reusing number operators for bigints (overloading) ............ 156 18.5 The wrapper constructor BigInt ....................... 160 18.6 Coercing bigints to other primitive types .................. 162 18.7 TypedArrays and DataView operations for 64-bit values .......... 162 18.8 Bigints and JSON ............................... 162 18.9 FAQ: Bigints .................................. 163 19 Unicode – a brief introduction (advanced) 165 19.1 Code points vs. code units .......................... 165 19.2 Encodings used in web development: UTF-16 and UTF-8 ......... 168 19.3 Grapheme clusters – the real characters ................... 168 20 Strings 171 20.1 Plain string literals .............................. 172 20.2 Accessing characters and code points .................... 172 20.3 String concatenation via + .......................... 173 20.4 Converting to string ............................. 173 20.5 Comparing strings .............................. 175 20.6 Atoms of text: Unicode characters, JavaScript characters, grapheme clusters176 20.7 Quick reference: Strings ........................... 178 21 Using template literals and tagged templates 187 21.1 Disambiguation: “template” ......................... 187 21.2 Template literals ............................... 188 21.3 Tagged templates ............................... 189 21.4 Examples of tagged templates (as provided via libraries)