Book description
Why reinvent the wheel every time you run into a problem with JavaScript? This cookbook is chock-full of code recipes for common programming tasks, along with techniques for building apps that work in any browser. You'll get adaptable code samples that you can add to almost any project--and you'll learn more about JavaScript in the process.
The recipes in this book take advantage of the latest features in ECMAScript 2020 and beyond and use modern JavaScript coding standards. You'll learn how to:
- Set up a productive development environment with a code editor, linter, and test server
- Work with JavaScript data types, such as strings, arrays, and BigInts
- Improve your understanding of JavaScript functions, including arrow functions, closures, and generators
- Apply object-oriented programming concepts like classes and inheritance
- Work with rich media in JavaScript, including audio, video, and SVGs
- Manipulate HTML markup and CSS styles
- Use JavaScript anywhere with Node.js
- Access and manipulate remote data with REST, GraphQL, and Fetch
- Get started with the popular Express application-building framework
- Perform asynchronous operations with Promises, async/await, and web workers
Publisher resources
Table of contents
- Preface
- I. The JavaScript Language
-
1. Setting Up a Development Environment
- Choosing a Code Editor
- Using the Developer Console in Your Browser
- Running Blocks of Code in the Developer Console
- Using Strict Mode to Catch Common Mistakes
- Filling in HTML Boilerplate with Emmet Shortcuts
- Installing the npm Package Manager (with Node.js)
- Downloading a Package with npm
- Updating a Package with npm
- Setting Up a Local Test Server
- Enforcing Code Standards with a Linter
- Styling Code Consistently with a Formatter
- Experimenting in a JavaScript Playground
-
2. Strings and Regular Expressions
- Checking for an Existing, Nonempty String
- Converting a Numeric Value to a Formatted String
- Inserting Special Characters
- Inserting Emojis
- Using Template Literals for Clearer String Concatenation
- Performing a Case-Insensitive String Comparison
- Checking If a String Contains a Specific Substring
- Replacing All Occurrences of a String
- Replacing HTML Tags with Named Entities
- Using a Regular Expression to Replace Patterns in a String
- Extracting a List from a String
- Finding All Instances of a Pattern
- Removing Whitespace from the Beginning and End of a String
- Converting the First Letter of a String to Uppercase
- Validating an Email Address
-
3. Numbers
- Generating Random Numbers
- Generating Cryptographically Secure Random Numbers
- Rounding to a Specific Decimal Place
- Preserving Accuracy in Decimal Values
- Converting a String to a Number
- Converting a Decimal to a Hexadecimal Value
- Converting Between Degrees and Radians
- Calculating the Length of a Circular Arc
- Manipulating Very Large Numbers with BigInt
- 4. Dates
-
5. Arrays
- Checking If an Object Is an Array
- Iterating Over All the Elements in an Array
- Checking If Two Arrays Are Equal
- Breaking Down an Array into Separate Variables
- Passing an Array to a Function That Expects a List of Values
- Cloning an Array
- Merging Two Arrays
- Copying a Portion of an Array by Position
- Extracting Array Items That Meet Specific Criteria
- Emptying an Array
- Removing Duplicate Values
- Flattening a Two-Dimensional Array
- Searching Through an Array for Exact Matches
- Searching Through an Array for Items That Meet Specific Criteria
- Removing or Replacing Array Elements
- Sorting an Array of Objects by a Property Value
- Transforming Every Element of an Array
- Combining an Arrayâs Values in a Single Calculation
- Validating Array Contents
- Creating a Collection of Nonduplicated Values
- Creating a Key-Indexed Collection of Items
-
6. Functions
- Passing a Function as an Argument to Another Function
- Using Arrow Functions
- Providing a Default Parameter Value
- Creating a Function That Accepts Unlimited Arguments
- Using Named Function Parameters
- Creating a Function That Stores its State with a Closure
- Creating a Generator Function That Yields Multiple Values
- Reducing Redundancy by Using Partial Application
- Fixing this with Function Binding
- Implementing a Recursive Algorithm
-
7. Objects
- Checking if an Object Is a Certain Type
- Using an Object Literal to Bundle Data
- Checking If an Object Has a Property
- Iterating Over All the Properties of an Object
- Testing for an Empty Object
- Merging the Properties of Two Objects
- Customizing the Way a Property Is Defined
- Preventing Any Changes to an Object
- Intercepting and Changing Actions on an Object with a Proxy
- Cloning an Object
- Making a Deep Copy of an Object
- Creating Absolutely Unique Object Property Keys
- Creating Enums with Symbol
-
8. Classes
- Creating a Reusable Class
- Adding Properties to a Class
- Giving a Class a Better String Representation
- Using the Constructor Pattern to Make a Custom Class
- Supporting Method Chaining in Your Class
- Adding Static Methods to a Class
- Using a Static Method to Create Objects
- Inheriting Functionality from Another Class
- Organizing Your JavaScript Classes with Modules
-
9. Asynchronous Programming
- Updating the Page During a Loop
- Using a Function That Returns a Promise
- Promisifying an Asynchronous Function That Uses a Callback
- Executing Multiple Promises Concurrently
- Waiting for a Promise to Finish with Await and Async
- Creating an Asynchronous Generator Function
- Using a Web Worker to Perform a Background Task
- Adding Progress Support to a Web Worker
- 10. Errors and Testing
- II. JavaScript in the Browser
- 11. Browser Tools
-
12. Working with HTML
- Accessing a Given Element and Finding Its Parent and Child Elements
- Traversing the Results from querySelectorAll() with forEach()
- Adding Click Functionality to an Element
- Finding All Elements That Share an Attribute
- Accessing All Elements of a Specific Type
- Discovering Child Elements Using the Selectors API
- Changing an Elementâs Class Value
- Setting an Elementâs Style Attribute
- Adding Text to a New Paragraph
- Inserting a New Element in a Specific DOM Location
- Checking If a Checkbox Is Checked
- Adding Up Values in an HTML Table
- Deleting Rows from an HTML Table
- Hiding Page Sections
- Creating Hover-Based Pop-Up Info Windows
- Validating Form Data
- Highlighting Form Errors and Accessibility
- Creating an Accessible Automatically Updated Region
-
13. Fetching Remote Data
- Requesting Remote Data with Fetch
- Using XMLHttpRequest
- Submitting a Form
- Populating a Selection List from the Server
- Parsing Returned JSON
- Fetching and Parsing XML
- Sending Binary Data and Loading into an Image
- Sharing HTTP Cookies Across Domains
- Using Websockets to Establish a Two-Way Communication Between Client and Server
- Long Polling a Remote Data Source
- 14. Data Persistence
- 15. Working with Media
-
16. Writing Web Applications
- Bundling JavaScript
- JavaScript and the Mobile Web
- Writing a Progressive Web Application
- Testing and Profiling a Progressive Web Application
- Getting the Value of the Current URL
- Redirecting a URL
- Copying Text to a Userâs Clipboard
- Enabling a Mobile-Like Notification in the Desktop Browser
- Loading a File Locally in the Browser
- Extending the Possible with Web Components
- Choosing a Front-End Framework
- III. Node.js
-
17. Node Basics
- Managing Node Versions with Node Version Manager
- Responding to a Simple Browser Request
- Interactively Trying Out Node Code Snippets with REPL
- Reading and Writing File Data
- Getting Input from the Terminal
- Getting the Path to the Current Script
- Working with Node Timers and Understanding the Node Event Loop
- 18. Node Modules
-
19. Managing Node
- Using Environment Variables
- Managing Callback Hell
- Accessing Command-Line Functionality Within a Node Application
- Passing Command-Line Arguments
- Creating a Command-Line Utility with Help from Commander
- Keeping a Node Instance Up and Running
- Monitoring Application Changes and Restarting During Local Development
- Scheduling Repeat Tasks
- Testing the Performance and Capability of Your WebSockets Application
- 20. Remote Data
- 21. Building Web Applications with Express
- Index
Product information
- Title: JavaScript Cookbook, 3rd Edition
- Author(s):
- Release date: July 2021
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781492055709
You might also like
book
Learning JavaScript, 3rd Edition
This is an exciting time to learn JavaScript. Now that the latest JavaScript specification—ECMAScript 6.0 (ES6)—has …
book
Eloquent JavaScript, 3rd Edition
JavaScript lies at the heart of almost every modern web application, from social apps like Twitter …
book
Head First JavaScript Programming
What will you learn from this book? This brain-friendly guide teaches you everything from JavaScript language …
video
Learn JavaScript
4+ Hours of Video Instruction JavaScript started as a basic language for performing simple computations on …