Simplifying JavaScript
Writing Modern JavaScript with ES5, ES6, and Beyond
by Joe Morgan
Version: P1.0 (April 2018)
Copyright 2018 The Pragmatic Programmers, LLC. This book is licensed to the individual who purchased it. We don't copy-protect it because that would limit your ability to use it for your own purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copy with other members of your team, with friends, or via file sharing services. Thanks.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
About the Pragmatic Bookshelf
The Pragmatic Bookshelf is an agile publishing company. Were here because we want to improve the lives of developers. We do this by creating timely, practical titles, written by programmers for programmers.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
Our ebooks do not contain any Digital Restrictions Management, and have always been DRM-free. We pioneered the beta book concept, where you can purchase and read a book while its still being written, and provide feedback to the author to help make a better book for everyone. Free resources for all purchasers include source code downloads (if applicable), errata and discussion forums, all available on the book's home page at pragprog.com. Were here to make your life easier.
New Book Announcements
Want to keep up on our latest titles and announcements, and occasional special offers? Just create an account on pragprog.com (an email address and a password is all it takes) and select the checkbox to receive newsletters. You can also follow us on twitter as @pragprog.
About Ebook Formats
If you buy directly from pragprog.com, you get ebooks in all available formats for one price. You can synch your ebooks amongst all your devices (including iPhone/iPad, Android, laptops, etc.) via Dropbox. You get free updates for the life of the edition. And, of course, you can always come back and re-download your books when needed. Ebooks bought from the Amazon Kindle store are subject to Amazon's polices. Limitations in Amazon's file format may cause ebooks to display differently on different devices. For more information, please see our FAQ at pragprog.com/frequently-asked-questions/ebooks. To learn more about this book and access the free resources, go to https://pragprog.com/book/es6tips, the book's homepage.
Thanks for your continued support,
Andy Hunt
The Pragmatic Programmers
The team that produced this book includes: Andy Hunt (Publisher) Janet Furlow (VP of Operations) Brian MacDonald (Managing Editor) Jacquelyn Carter (Supervising Editor) Andrea Stewart (Development Editor) Nancy Rapoport (Copy Editor) Potomac Indexing, LLC (Indexing) Gilson Graphics (Layout)
For customer support, please contact .
For international rights, please contact .
To Bob and Eric for giving me a foundation
Table of Contents
Copyright 2018, The Pragmatic Bookshelf.
Early praise for Simplifying JavaScript
You should probably jot down each Tip from this book on Post-It notesand plaster them all over your desk area. These simple, practical bits ofadvice will have a profound effect on simplifying your JavaScript.
Kyle Simpson |
Author of You Dont KnowJS and Open Web Evangelist |
Joe provides a clear, organized path to mastering key JavaScriptconcepts. This isnt a boring textbook. Its a playbook filled with practical,real-world approaches to writing modular, maintainable, and modern JavaScript.
Cory House |
Pluralsight Author and International Speaker and Consultant, reactjsconsulting.com |
This book is great for experienced developers whose past experience with JavaScript has left a bad taste in their mouth. Modern JavaScript, when understood through this book, is sure to turn former critics into advocates.
New developers will also benefit from thorough explanations that are in laymans termsnot the overly technical jargon that typically acts as a barrier to entry with many technical books.
Sara Heins |
International Speaker, Django Girls Kansas City ProgramDirector, and Lead Web Developer, Big 6 Media |
This book is ideal for beginners as well as intermediate and beyond.
Shreerang Patwardhan |
Author of Mastering jQuery Mobile, Senior Software Engineer, Macys Inc. |
I would definitely recommend this book to others on my team. I believe that most, if not all, of my team could benefit from reading this book.
Nick McGinness |
Software Engineer, Direct Supply |
Introduction
Ever get the feeling youve woken up and everythings different? If youvelooked at modern JavaScript, you may feel like you woke up in a new worldwhere nothings the same.
Yesterday, you were tweaking a simple jQuery accordion. Today, you look atsome JavaScript code and the ubiquitous jQuery $ operator is gone. In itsplace are lots of strange dots ( ... ) and strange symbols ( => ) (the spreadoperator and arrow functions, as youll soon learn). Sure, some things lookfamiliar, such as class , but even that seems to be unpredictable (where are theprivate methods?).
Are you perplexed? Maybe a little excited? Well, I have good news for you:Everything has changed for the better.
When the ECMAScript 6 spec was releasedES6 for shortJavaScriptcode changed dramatically. The changes were so substantial that after readingthis book youll be able to tell at a glance which code was written withpre-ES6 syntax and which code was written with post-ES6 syntax. JavaScript nowis different. Modern JavaScript is any code composed with post-ES6 syntax.
Modern JavaScript is a pleasure to write. Im a long-time unrepentantJavaScript fan, but Ill admit that I found the old syntax a little clunky attimes, and it was pretty hard to defend. Modern JavaScript is better. But itschanged significantly, and catching up can be hard.
How To Use This Book
In this book, youll learn to write modern JavaScript from the ground up. ButIm not going to throw a bunch of syntax at you. I want you to learn to thinkin terms of contemporary JavaScript. Youll see every piece of syntaxwith a recommendation for not just how to use it, but when to use it.
Also, Im not going to run through every minor syntax change. I know your timeis valuable, so Ill only show you syntax that has high impact and thatssomething youll use over and over again. You can deal with edge cases whenthey arise. This book will give you the best features and nothing else.