📄 Page
1
JavaScript Recipes A Problem-Solution Approach — Russ Ferguson Keith Cirkel
📄 Page
2
JavaScript Recipes A Problem-Solution Approach Russ Ferguson Keith Cirkel
📄 Page
3
JavaScript Recipes: A Problem-Solution Approach Russ Ferguson Keith Cirkel Ocean, New Jersey, USA London, United Kingdom ISBN-13 (pbk): 978-1-4302-6106-3 ISBN-13 (electronic): 978-1-4302-6107-0 DOI 10.1007/978-1-4302-6107-0 Library of Congress Control Number: 2016960744 Copyright © 2017 by Russ Ferguson and Keith Cirkel This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director: Welmoed Spahr Acquisitions Editor: Louise Corrigan Technical Reviewer: Luca Mezzalira Editorial Board: Steve Anglin, Pramila Balan, Laura Berendson, Aaron Black, Louise Corrigan, Jonathan Gennick, Todd Green, Robert Hutchinson, Celestin Suresh John, Nikhil Karkal, James Markham, Susan McDermott, Matthew Moodie, Natalie Pao, Gwenan Spearing Coordinating Editor: Nancy Chen Copy Editor: Kezia Endsley Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springer.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary materials referenced by the author in this text are available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/. Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter. Printed on acid-free paper
📄 Page
4
This space is always reserved for my brother Rodd and my Dad. You guys are severely missed. —Russ
📄 Page
5
v Contents at a Glance About the Authors ���������������������������������������������������������������������������������������������������� lv Acknowledgments ������������������������������������������������������������������������������������������������� lvii Introduction ������������������������������������������������������������������������������������������������������������ lix ■Chapter 1: Working with JavaScript Datatypes ����������������������������������������������������� 1 ■Chapter 2: Working with Expressions ����������������������������������������������������������������� 49 ■Chapter 3: Working with Strings ������������������������������������������������������������������������� 91 ■Chapter 4: Working with Numbers and Math ���������������������������������������������������� 137 ■Chapter 5: Working with Bitwise Operations Against 32-Bit Integers �������������� 151 ■Chapter 6: Dates and Times ������������������������������������������������������������������������������� 173 ■Chapter 7: Working with Arrays ������������������������������������������������������������������������ 181 ■Chapter 8: Working with Arrays in Loops ���������������������������������������������������������� 195 ■Chapter 9: Working with Objects ����������������������������������������������������������������������� 207 ■Chapter 10: Working with Sets �������������������������������������������������������������������������� 225 ■Chapter 11: Working with Maps ������������������������������������������������������������������������ 233 ■Chapter 12: Working with Functions ����������������������������������������������������������������� 239 ■Chapter 13: Working with Generators ��������������������������������������������������������������� 255 ■Chapter 15: Working with Symbols ������������������������������������������������������������������� 267 ■Chapter 16: Working with Proxies ��������������������������������������������������������������������� 277
📄 Page
6
■ Contents at a GlanCe vi ■Chapter 17: Working with Classes �������������������������������������������������������������������� 281 ■Chapter 18: Working with Events ���������������������������������������������������������������������� 289 ■Chapter 19: Debugging and Handling Exceptions ���������������������������������������������� 295 ■Chapter 20: Working with Regular Expressions ������������������������������������������������ 301 ■Chapter 21: Working with Asynchronous Control Flow and Promises �������������� 309 Index ��������������������������������������������������������������������������������������������������������������������� 341
📄 Page
7
vii Contents About the Authors ���������������������������������������������������������������������������������������������������� lv Acknowledgments ������������������������������������������������������������������������������������������������� lvii Introduction ������������������������������������������������������������������������������������������������������������ lix ■Chapter 1: Working with JavaScript Datatypes ����������������������������������������������������� 1 Assigning Variables with the var Statement ��������������������������������������������������������������������� 1 Problem �������������������������������������������������������������������������������������������������������������������������������������������������� 1 Solution��������������������������������������������������������������������������������������������������������������������������������������������������� 1 The Code ������������������������������������������������������������������������������������������������������������������������������������������������� 1 How It Works ������������������������������������������������������������������������������������������������������������������������������������������� 1 Solving Errors with Variable Names ��������������������������������������������������������������������������������� 2 Problem �������������������������������������������������������������������������������������������������������������������������������������������������� 2 Solution��������������������������������������������������������������������������������������������������������������������������������������������������� 2 The Code ������������������������������������������������������������������������������������������������������������������������������������������������� 3 How It Works ������������������������������������������������������������������������������������������������������������������������������������������� 3 Solving Reference Errors When Declaring Variables Inside a Function’s Scope ������������� 3 Problem �������������������������������������������������������������������������������������������������������������������������������������������������� 3 Solution��������������������������������������������������������������������������������������������������������������������������������������������������� 4 The Code ������������������������������������������������������������������������������������������������������������������������������������������������� 4 How It Works ������������������������������������������������������������������������������������������������������������������������������������������� 4 Assigning Variables with the Let Statement ��������������������������������������������������������������������� 6 Problem �������������������������������������������������������������������������������������������������������������������������������������������������� 6 Solution��������������������������������������������������������������������������������������������������������������������������������������������������� 6 The Code ������������������������������������������������������������������������������������������������������������������������������������������������� 6 How It Works ������������������������������������������������������������������������������������������������������������������������������������������� 7
📄 Page
8
■ Contents viii Assigning Constants with the Const Statement ��������������������������������������������������������������� 7 Problem �������������������������������������������������������������������������������������������������������������������������������������������������� 7 Solution��������������������������������������������������������������������������������������������������������������������������������������������������� 8 The Code ������������������������������������������������������������������������������������������������������������������������������������������������� 8 How It Works ������������������������������������������������������������������������������������������������������������������������������������������� 8 Creating and Using Literals ���������������������������������������������������������������������������������������������� 8 Problem �������������������������������������������������������������������������������������������������������������������������������������������������� 9 Solution��������������������������������������������������������������������������������������������������������������������������������������������������� 9 The Code ������������������������������������������������������������������������������������������������������������������������������������������������� 9 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 10 Creating Types Using Their Constructors ������������������������������������������������������������������������ 10 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 10 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 10 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 10 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 11 Determine If a Variable Has Been Defined Using an Equality Operator �������������������������� 11 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 11 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 11 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 11 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 12 Determining If a Variable Has Been Declared Using typeof( ) ����������������������������������������� 12 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 12 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 12 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 12 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 13 Determining If a Function Defines and Returns a Value ������������������������������������������������� 13 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 13 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 13 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 13 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 15
📄 Page
9
■ Contents ix Determining If a Defined Variable Has a Value Using Equality Operators �������������������� 15 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 15 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 16 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 16 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 17 Performing Operations If a Defined Variable Has a Value ����������������������������������������������� 17 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 17 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 17 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 17 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 18 What’s the Difference Between Null and Undefined?����������������������������������������������������� 18 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 18 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 18 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 19 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 19 Coercing a Boolean Variable Using the Boolean Constructor ����������������������������������������� 19 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 19 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 19 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 20 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 20 Determining If a Boolean Variable Is Initialized �������������������������������������������������������������� 20 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 20 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 21 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 21 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 22 Valid Representations of the False and True Values ������������������������������������������������������� 22 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 22 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 22 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 22 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 24
📄 Page
10
■ Contents x Coercing a String Using the String Constructor ������������������������������������������������������������� 24 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 24 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 24 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 24 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 25 Determining If a Variable Is a String ������������������������������������������������������������������������������� 25 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 25 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 25 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 25 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 26 Coercing a Numeric Value Using the Number Constructor ��������������������������������������������� 26 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 26 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 26 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 26 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 27 Creating a Numeric Value Using Number Literals ���������������������������������������������������������� 27 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 27 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 27 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 28 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 29 Determining If a Defined Variable Is a Number �������������������������������������������������������������� 29 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 29 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 29 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 29 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 30 Dealing with NaN and Infinity ����������������������������������������������������������������������������������������� 30 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 30 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 31 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 31 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 32
📄 Page
11
■ Contents xi Generating a Date Using the Date Object ����������������������������������������������������������������������� 32 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 32 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 32 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 32 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 33 Generating a Date with a Date String ����������������������������������������������������������������������������� 33 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 33 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 33 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 33 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 34 Determining If a Defined Variable Is a Date �������������������������������������������������������������������� 34 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 34 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 35 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 35 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 35 Creating an Object and Assigning Properties ����������������������������������������������������������������� 36 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 36 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 36 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 36 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 37 Determining If a Defined Variable Is an Object ��������������������������������������������������������������� 37 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 37 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 37 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 38 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 38 Determining If an Object Is an Instance ������������������������������������������������������������������������� 38 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 38 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 38 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 39 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 40
📄 Page
12
■ Contents xii Determining an Object’s Direct Instance with the Constructor Property ������������������������ 40 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 40 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 40 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 40 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 41 Determining If Something Is a Plain Object �������������������������������������������������������������������� 42 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 42 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 42 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 42 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 43 Creating an Array and Assigning and Retrieving Values ������������������������������������������������� 43 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 43 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 43 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 43 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 44 Choosing Between Objects and Arrays �������������������������������������������������������������������������� 44 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 44 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 44 Creating a Regular Expression Literal ���������������������������������������������������������������������������� 45 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 45 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 45 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 45 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 46 Injecting Variables into a String with Template Literals ������������������������������������������������� 46 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 46 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 46 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 47 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 47 ■Chapter 2: Working with Expressions ����������������������������������������������������������������� 49 Performing an Addition with the + Operator ������������������������������������������������������������������ 49 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 49 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 49
📄 Page
13
■ Contents xiii The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 49 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 50 Performing a Subtraction with the - Operator ���������������������������������������������������������������� 50 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 50 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 50 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 50 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 51 Performing Multiplication with the * Operator ��������������������������������������������������������������� 51 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 51 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 51 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 51 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 52 Performing Division with the / Operator ������������������������������������������������������������������������� 52 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 52 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 52 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 52 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 52 Getting the Remainder of a Division Operation with the Modulo (%) Operator �������������� 53 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 53 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 53 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 53 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 53 Determining If a Value Is Less Than Another with the < Operator ���������������������������������� 54 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 54 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 54 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 54 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 55 Determining If a Value Is Greater than Another with the > Operator ������������������������������ 55 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 55 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 55 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 55 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 56
📄 Page
14
■ Contents xiv Determining If a Value Is Less than or Equal to Another with the <= Operator ����������� 56 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 56 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 56 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 56 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 57 Determining If a Value Is Greater than or Equal to Another with the >= Operator ��������� 57 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 57 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 57 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 58 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 58 Determining If a Value Is Equivalent to Another Using the == Operator ������������������������ 59 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 59 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 59 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 59 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 60 Determining If a Value Is Strictly Equal to Another Using the === Operator ��������������� 62 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 62 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 62 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 62 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 63 Determining If a Value Is Not Equivalent to Another Using the != Operator ������������������� 64 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 64 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 64 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 65 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 66 Determining If a Value Is Strictly Not Equal to Another Using the !== Operator ������������ 66 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 66 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 66 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 67 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 68
📄 Page
15
■ Contents xv Using the Increment (++) Operator to Increase the Value of a Number Variable ����������� 69 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 69 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 69 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 70 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 70 Using the Decrement (--) Operator to Decrease the Value of a Number Variable ����������� 70 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 70 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 71 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 71 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 71 Using the Unary Addition (+) Operator to Coerce an Object to a Number ���������������������� 71 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 71 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 72 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 72 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 72 Using the Unary Subtraction (-) Operator to Flip the Sign of a Number ������������������������� 72 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 72 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 73 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 73 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 73 Using the Logical NOT (!) Operator to Toggle a Boolean Value ��������������������������������������� 73 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 73 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 73 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 74 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 74 Deleting an Object, Property, or Array Element with the Delete Operator ���������������������� 74 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 74 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 74 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 75 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 75
📄 Page
16
■ Contents xvi Evaluating an Expression Without a Return Value with the Void Operator ��������������������� 76 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 76 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 76 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 76 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 77 Determining the Type of a Variable with the typeof Operator ����������������������������������������� 77 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 77 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 77 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 78 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 78 Using instanceof to Determine If a Variable Is an Instance of an Object ������������������������ 79 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 79 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 79 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 79 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 81 Finding Properties in an Object with the in Operator ����������������������������������������������������� 81 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 81 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 81 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 82 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 83 Using Logical AND (&&) for Combining Boolean Expressions in an if Statement, with Short-Circuit Evaluation ����������������������������������������������������������������������������������������� 83 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 83 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 83 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 83 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 84 Using Logical OR (||) Short-Circuit Evaluation in an if Statement ����������������������������������� 84 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 84 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 84 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 85 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 86
📄 Page
17
■ Contents xvii Simplifying Variable Assignments Using the Conditional (?) Operator ��������������������������� 86 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 86 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 86 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 86 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 87 Specifying Multiple Expressions Using the Comma Operator ���������������������������������������� 88 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 88 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 88 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 88 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 89 ■Chapter 3: Working with Strings ������������������������������������������������������������������������� 91 Understanding Unicode Strings �������������������������������������������������������������������������������������� 91 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 91 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 91 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 91 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 92 Using Special Characters (Escape Codes) in Strings������������������������������������������������������ 93 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 93 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 93 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 93 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 93 Comparing Two Strings for Equality ������������������������������������������������������������������������������� 93 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 93 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 94 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 94 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 94 Determining a String’s Length ���������������������������������������������������������������������������������������� 95 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 95 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 95 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 95 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 95
📄 Page
18
■ Contents xviii Concatenating Strings with the + Operator�������������������������������������������������������������������� 96 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 96 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 96 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 96 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 96 Getting a Single Character from a String ����������������������������������������������������������������������� 97 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 97 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 97 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 97 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 97 Creating a String of UTF Character Code Units with fromCharCode( ) ���������������������������� 98 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 98 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 98 The Code ����������������������������������������������������������������������������������������������������������������������������������������������� 98 How It Works ����������������������������������������������������������������������������������������������������������������������������������������� 99 Creating a String of UTF Code Points with fromCodePoint( ) ������������������������������������������ 99 Problem ������������������������������������������������������������������������������������������������������������������������������������������������ 99 Solution������������������������������������������������������������������������������������������������������������������������������������������������� 99 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 100 How It Works ��������������������������������������������������������������������������������������������������������������������������������������� 100 Getting a Single Character’s UTF Code Unit from a String with charCodeAt( ) ������������� 100 Problem ���������������������������������������������������������������������������������������������������������������������������������������������� 100 Solution����������������������������������������������������������������������������������������������������������������������������������������������� 101 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 101 How It Works ��������������������������������������������������������������������������������������������������������������������������������������� 101 Getting a Single Character’s UTF Code Point from a string with codePointAt( ) ����������� 102 Problem ���������������������������������������������������������������������������������������������������������������������������������������������� 102 Solution����������������������������������������������������������������������������������������������������������������������������������������������� 102 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 102 How It Works ��������������������������������������������������������������������������������������������������������������������������������������� 103
📄 Page
19
■ Contents xix Iterating Over a String’s code Units Using for���in ��������������������������������������������������������� 103 Problem ���������������������������������������������������������������������������������������������������������������������������������������������� 103 Solution����������������������������������������������������������������������������������������������������������������������������������������������� 103 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 103 How It Works ��������������������������������������������������������������������������������������������������������������������������������������� 103 Iterating Over a String’s Code Points Using for���of������������������������������������������������������� 104 Problem ���������������������������������������������������������������������������������������������������������������������������������������������� 104 Solution����������������������������������������������������������������������������������������������������������������������������������������������� 104 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 104 How It Works ��������������������������������������������������������������������������������������������������������������������������������������� 105 Repeating a String with repeat( ) ���������������������������������������������������������������������������������� 105 Problem ���������������������������������������������������������������������������������������������������������������������������������������������� 105 Solution����������������������������������������������������������������������������������������������������������������������������������������������� 105 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 105 How It Works ��������������������������������������������������������������������������������������������������������������������������������������� 106 Determining If a String Contains a Smaller String Using contains( ) ���������������������������� 106 Problem ���������������������������������������������������������������������������������������������������������������������������������������������� 106 Solution����������������������������������������������������������������������������������������������������������������������������������������������� 106 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 106 How It Works ��������������������������������������������������������������������������������������������������������������������������������������� 107 Determining If a String Starts with a Smaller String using startsWith( ) ���������������������� 108 Problem ���������������������������������������������������������������������������������������������������������������������������������������������� 108 Solution����������������������������������������������������������������������������������������������������������������������������������������������� 108 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 108 How It Works ��������������������������������������������������������������������������������������������������������������������������������������� 109 Determining If a String Ends with a Smaller String Using endsWith( ) ������������������������� 110 Problem ���������������������������������������������������������������������������������������������������������������������������������������������� 110 Solution����������������������������������������������������������������������������������������������������������������������������������������������� 110 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 110 How It Works ��������������������������������������������������������������������������������������������������������������������������������������� 111
📄 Page
20
■ Contents xx Finding the Index of an Occurring Substring with indexOf( ) ���������������������������������������� 112 Problem ���������������������������������������������������������������������������������������������������������������������������������������������� 112 Solution����������������������������������������������������������������������������������������������������������������������������������������������� 112 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 112 How It Works ��������������������������������������������������������������������������������������������������������������������������������������� 113 Finding the Index of the Last Occurrence of a Substring with lastIndexOf( ) ���������������� 113 Problem ���������������������������������������������������������������������������������������������������������������������������������������������� 113 Solution����������������������������������������������������������������������������������������������������������������������������������������������� 113 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 113 How It Works ��������������������������������������������������������������������������������������������������������������������������������������� 114 Finding Many Matches of a Substring with match( ) ���������������������������������������������������� 114 Problem ���������������������������������������������������������������������������������������������������������������������������������������������� 114 Solution����������������������������������������������������������������������������������������������������������������������������������������������� 114 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 115 How It Works ��������������������������������������������������������������������������������������������������������������������������������������� 115 Replacing Parts of a String with replace( ) ������������������������������������������������������������������� 115 Problem ���������������������������������������������������������������������������������������������������������������������������������������������� 115 Solution����������������������������������������������������������������������������������������������������������������������������������������������� 115 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 116 How It Works ��������������������������������������������������������������������������������������������������������������������������������������� 116 Searching a String Using a Regular Expression with search( ) ��������������������������������������� 116 Problem ���������������������������������������������������������������������������������������������������������������������������������������������� 116 Solution����������������������������������������������������������������������������������������������������������������������������������������������� 117 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 117 How It Works ��������������������������������������������������������������������������������������������������������������������������������������� 117 Getting a Substring Form a String with slice( ) ������������������������������������������������������������� 118 Problem ���������������������������������������������������������������������������������������������������������������������������������������������� 118 Solution����������������������������������������������������������������������������������������������������������������������������������������������� 118 The Code ��������������������������������������������������������������������������������������������������������������������������������������������� 118 How It Works ��������������������������������������������������������������������������������������������������������������������������������������� 118