Transcription

2

JavaScript for impatient programmers(ES2021 edition)Dr. Axel Rauschmayer2021“An exhaustive resource, yet cuts out the fluff that clutters manyprogramming books – with explanations that are understandable and tothe point, as promised by the title! The quizzes and exercises are a veryuseful feature to check and lock in your knowledge. And you candefinitely tear through the book fairly quickly, to get up and running inJavaScript.”— 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 RauschmayerCover by Fran CayeAll rights reserved. This book or any portion thereof may not be reproduced or used inany manner whatsoever without the express written permission of the publisher exceptfor the use of brief quotations in a book review or scholarly journal.ISBN 978-1-09-121009-7exploringjs.com

ContentsIBackground1Before you buy the book1.1 About the content . . . . . . . . .1.2 Previewing and buying this book1.3 About the author . . . . . . . . .1.4 Acknowledgements . . . . . . . .23459.FAQ: book and supplementary material2.1 How to read this book . . . . . . .2.2 I own a digital version . . . . . . .2.3 I own the print version . . . . . . .2.4 Notations and conventions . . . . .1111121213.1515161717History and evolution of JavaScript3.1 How JavaScript was created . . . . . . .3.2 Standardizing JavaScript . . . . . . . . .3.3 Timeline of ECMAScript versions . . . .3.4 Ecma Technical Committee 39 (TC39) . .3.5 The TC39 process . . . . . . . . . . . . .3.6 FAQ: TC39 process . . . . . . . . . . . .3.7 Evolving JavaScript: Don’t break the web.1919202021212323New JavaScript features4.1 New in ECMAScript 20214.2 New in ECMAScript 20204.3 New in ECMAScript 20194.4 New in ECMAScript 20184.5 New in ECMAScript 20174.6 New in ECMAScript 20164.7 Source of this chapter . . .2525252626262727FAQ: JavaScript5.1 What are good references for JavaScript? . . . . . . . . . . . . . .5.2 How do I find out what JavaScript features are supported where?5.3 Where can I look up what features are planned for JavaScript? . .5.4 Why does JavaScript fail silently so often? . . . . . . . . . . . . .2929293030.3.

4CONTENTS5.55.6II6Why can’t we clean up JavaScript, by removing quirks and outdated features? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .How can I quickly try out a piece of JavaScript code? . . . . . . . . . . .First steps303031Using JavaScript: the big picture6.1 What are you learning in this book? . .6.2 The structure of browsers and Node.js6.3 JavaScript references . . . . . . . . . .6.4 Further reading . . . . . . . . . . . . .3333333434Syntax7.1 An overview of JavaScript’s syntax .7.2 (Advanced) . . . . . . . . . . . . . .7.3 Identifiers . . . . . . . . . . . . . . .7.4 Statement vs. expression . . . . . . .7.5 Ambiguous syntax . . . . . . . . . .7.6 Semicolons . . . . . . . . . . . . . .7.7 Automatic semicolon insertion (ASI)7.8 Semicolons: best practices . . . . . .7.9 Strict mode vs. sloppy mode . . . . .353643434446474849508Consoles: interactive JavaScript command lines8.1 Trying out JavaScript code . . . . . . . . . . . . . . . . . . . . . . . . . .8.2 The console.* API: printing data and more . . . . . . . . . . . . . . . .5353559Assertion API9.1 Assertions in software development . . .9.2 How assertions are used in this book . .9.3 Normal comparison vs. deep comparison9.4 Quick reference: module assert . . . . .595959606110 Getting started with quizzes and exercises10.1 Quizzes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10.2 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10.3 Unit tests in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . .65656566III717.Variables and values11 Variables and assignment11.1 let . . . . . . . . . . . . . . . . . . .11.2 const . . . . . . . . . . . . . . . . .11.3 Deciding between const and let . .11.4 The scope of a variable . . . . . . . .11.5 (Advanced) . . . . . . . . . . . . . .11.6 Terminology: static vs. dynamic . . .11.7 Global variables and the global object.7374747575777778

5CONTENTS11.8 Declarations: scope and activation . . . . . . . . . . . . . . . . . . . . . .11.9 Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .808412 Values12.1 What’s a type? . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12.2 JavaScript’s type hierarchy . . . . . . . . . . . . . . . . . . . . . .12.3 The types of the language specification . . . . . . . . . . . . . . .12.4 Primitive values vs. objects . . . . . . . . . . . . . . . . . . . . . .12.5 The operators typeof and instanceof: what’s the type of a value?12.6 Classes and constructor functions . . . . . . . . . . . . . . . . . .12.7 Converting between types . . . . . . . . . . . . . . . . . . . . . .13 Operators13.1 Making sense of operators13.2 The plus operator ( ) . . .13.3 Assignment operators . .13.4 Equality: vs. . . . .13.5 Ordering operators . . . .13.6 Various other operators . .97. 97. 98. 99. 100. 103. 104IV.Primitive values878788888991939410514 The non-values undefined and null14.1 undefined vs. null . . . . . . . . . . . . . . . . . . . . . . . .14.2 Occurrences of undefined and null . . . . . . . . . . . . . . .14.3 Checking for undefined or null . . . . . . . . . . . . . . . . .14.4 The nullish coalescing operator (?) for default values [ES2020]14.5 undefined and null don’t have properties . . . . . . . . . . .14.6 The history of undefined and null . . . . . . . . . . . . . . .10710710810910911211315 Booleans15.1 Converting to boolean . . . . . . .15.2 Falsy and truthy values . . . . . . .15.3 Truthiness-based existence checks .15.4 Conditional operator (? :) . . . . .15.5 Binary logical operators: And (x &&15.6 Logical Not (!) . . . . . . . . . . .11511511611711912012216 Numbers16.1 Numbers are used for both floating point numbers and integers16.2 Number literals . . . . . . . . . . . . . . . . . . . . . . . . . . .16.3 Arithmetic operators . . . . . . . . . . . . . . . . . . . . . . . .16.4 Converting to number . . . . . . . . . . . . . . . . . . . . . . .16.5 Error values . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16.6 The precision of numbers: careful with decimal fractions . . . .16.7 (Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16.8 Background: floating point precision . . . . . . . . . . . . . . .16.9 Integer numbers in JavaScript . . . . . . . . . . . . . . . . . . .123124124126129130132132132134. . . . . . . . . . . . .y), Or (x. . . . . y). . . .

6CONTENTS16.10Bitwise operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13716.11 Quick reference: numbers . . . . . . . . . . . . . . . . . . . . . . . . . . 13917 Math17.1 Data properties . . . . . . .17.2 Exponents, roots, logarithms17.3 Rounding . . . . . . . . . .17.4 Trigonometric Functions . .17.5 Various other functions . . .17.6 Sources . . . . . . . . . . .14514514614714815015118 Bigints – arbitrary-precision integers [ES2020] (advanced)18.1 Why bigints? . . . . . . . . . . . . . . . . . . . . . . .18.2 Bigints . . . . . . . . . . . . . . . . . . . . . . . . . . .18.3 Bigint literals . . . . . . . . . . . . . . . . . . . . . . .18.4 Reusing number operators for bigints (overloading) . .18.5 The wrapper constructor BigInt . . . . . . . . . . . . .18.6 Coercing bigints to other primitive types . . . . . . . .18.7 TypedArrays and DataView operations for 64-bit values18.8 Bigints and JSON . . . . . . . . . . . . . . . . . . . . .18.9 FAQ: Bigints . . . . . . . . . . . . . . . . . . . . . . . .15315315415515615916116216216319 Unicode – a brief introduction (advanced)19.1 Code points vs. code units . . . . . . . . . . . . . . . . . . . . . . . . . .19.2 Encodings used in web development: UTF-16 and UTF-8 . . . . . . . . .19.3 Grapheme clusters – the real characters . . . . . . . . . . . . . . . . . . .165166168169.20 Strings17120.1 Plain string literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17120.2 Accessing characters and code points . . . . . . . . . . . . . . . . . . . . 17220.3 String concatenation via . . . . . . . . . . . . . . . . . . . . . . . . . . 17320.4 Converting to string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17320.5 Comparing strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17520.6 Atoms of text: Unicode characters, JavaScript characters, grapheme clusters17620.7 Quick reference: Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 17821 Using template literals and tagged templates21.1 Disambiguation: “template” . . . . . . . . . . . . . . . .21.2 Template literals . . . . . . . . . . . . . . . . . . . . . .21.3 Tagged templates . . . . . . . . . . . . . . . . . . . . . .21.4 Examples of tagged templates (as provided via libraries)21.5 Raw string literals . . . . . . . . . . . . . . . . . . . . . .21.6 (Advanced) . . . . . . . . . . . . . . . . . . . . . . . . .21.7 Multiline template literals and indentation . . . . . . . .21.8 Simple templating via template literals . . . . . . . . . .18718718818919019219219219422 Symbols19722.1 Symbols are primitives that are also like objects . . . . . . . . . . . . . . 19822.2 The descriptions of symbols . . . . . . . . . . . . . . . . . . . . . . . . . 199

7CONTENTS22.3 Use cases for symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19922.4 Publicly known symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . 20222.5 Converting symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202VControl flow and data flow23 Control flow statements23.1 Controlling loops: break and continue23.2 Conditions of control flow statements .23.3 if statements [ES1] . . . . . . . . . . .23.4 switch statements [ES3] . . . . . . . .23.5 while loops [ES1] . . . . . . . . . . . .23.6 do-while loops [ES3] . . . . . . . . . .23.7 for loops [ES1] . . . . . . . . . . . . .23.8 for-of loops [ES6] . . . . . . . . . . .23.9 for-await-of loops [ES2018] . . . . .23.10 for-in loops (avoid) [ES1] . . . . . . .23.11 Recomendations for looping . . . . . .205.20720720920921021321421421521621721724 Exception handling24.1 Motivation: throwing and catching exceptions24.2 throw . . . . . . . . . . . . . . . . . . . . . .24.3 The try statement . . . . . . . . . . . . . . . .24.4 Error classes . . . . . . . . . . . . . . . . . . .21922022122122425 Callable values25.1 Kinds of functions . . . . . . . . . . . . . . . . .25.2 Ordinary functions . . . . . . . . . . . . . . . . .25.3 Specialized functions . . . . . . . . . . . . . . . .25.4 Summary: kinds of callable values . . . . . . . . .25.5 Returning values from functions and methods . .25.6 Parameter handling . . . . . . . . . . . . . . . . .25.7 Methods of functions: .call(), .apply(), .bind().22722722723123523623724226 Evaluating code dynamically:26.1 eval() . . . . . . . . . .26.2 new Function() . . . . .26.3 Recommendations . . .VI.eval(), new Function() (advanced)245. . . . . . . . . . . . . . . . . . . . . . . . . . . 246. . . . . . . . . . . . . . . . . . . . . . . . . . . 247. . . . . . . . . . . . . . . . . . . . . . . . . . . 247Modularity27 Modules27.1 Overview: syntax of ECMAScript modules27.2 JavaScript source code formats . . . . . . .27.3 Before we had modules, we had scripts . .27.4 Module systems created prior to ES6 . . .27.5 ECMAScript modules . . . . . . . . . . .27.6 Named exports and imports . . . . . . . .249.251252253253255257257

8CONTENTS27.7 Default exports and imports . . . . . . . . . . . . . . . . . .27.8 More details on exporting and importing . . . . . . . . . . .27.9 n