Completely revised and updated, this best-selling introduction to programming in JavaScript focuses on writing real applications.JavaScript lies at the heart of almost every modern web application, from social apps like Twitter to browser-based game frameworks like Phaser and Babylon. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.This much anticipated and thoroughly revised third edition of Eloquent JavaScript dives deep into the JavaScript language to show you how to write beautiful, effective code. It has been updated to reflect the current state of JavaScript and web browsers and includes brand-new material on features like class notation, arrow functions, iterators, async functions, template strings, and block scope. A host of new exercises have also been added to test your skills and keep you on track.As with previous editions, Haverbeke continues to teach through extensive examples and immerses you in code from the start, while exercises and full-chapter projects give you hands-on experience with writing your own programs. You start by learning the basic structure of the JavaScript language as well as control structures, functions, and data structures to help you write basic programs. Then you’ll learn about error handling and bug fixing, modularity, and asynchronous programming before moving on to web browsers and how JavaScript is used to program them. As you build projects such as an artificial life simulation, a simple programming language, and a paint program, you’ll learn how to:• Understand the essential elements of programming, including syntax, control, and data• Organize and clarify your code with object-oriented and functional programming techniques• Script the browser and make basic web applications• Use the DOM effectively to interact with browsers• Harness Node.js to build servers and utilitiesIsn’t it time you became fluent in the language of the Web?All source code is available online in an interactive sandbox, where you can edit the code, run it, and see its output instantly.Author Bio Marijn Haverbeke is a programming language enthusiast and polyglot. He's worked on a wide range of software systems, from databases to compilers to editors. He runs a small business around his open source projects. Brief Contents Contents in Detail Introduction On Programming Why Language Matters What Is JavaScript? Code, and What to Do with It Overview of This Book Typographic Conventions Part I: Language Chapter 1: Values, Types, and Operators Values Numbers Arithmetic Special Numbers Strings Unary Operators Boolean Values Comparison Logical Operators Empty Values Automatic Type Conversion Short-Circuiting of Logical Operators Summary Chapter 2: Program Structure Expressions and Statements Bindings Binding Names The Environment Functions The console.log Function Return Values Control Flow Conditional Execution while and do Loops Indenting Code for Loops Breaking Out of a Loop Updating Binding Succinctly Dispatching on a Value with switch Capitalization Comments Summary Exercises Looping a Triangle FizzBuzz Chessboard Chapter 3: Functions Defining a Function Bindings and Scope Nested Scope Functions as Value Declaration Notation Arrow Functions The Call Stack Optional Arguments Closure Recursion Growing Functions Functions and Side Effects Summary Exercises Minimum Recursion Bean Counting Chapter 4: Data Structures: Objects and Arrays The Weresquirrel Data Sets Properties Methods Objects Mutability The Lycanthrope's Log Computing Correlation Array Loops The Final Analysis Further Arrayology Strings and Their Properties Rest Parameters The Math Object Destructuring JSON Summary Exercises The Sum of a Range Reversing an Array A List Deep Comparison Chapter 5: Higher-Order Functions Abstraction Abstracting Repetition Higher-Order Functions Script Data Set Filtering Arrays Transforming with map Summarizing with reduce Composability Strings and Character Codes Recognizing Text Summary Exercises Flattening Your Own Loop Everything Dominant Writing Direction Chapter 6: The Secret Life of Objects Encapsulation Methods Prototypes Classes Class Notation Overriding Derived Properties Maps Polymorphism Symbols The Iterator Interface Getters, Setters, and Statics Inheritance The instanceof Operator Summary Exercises A Vector Type Groups Iterable Groups Borrowing a Method Chapter 7: Project: A Robot Meadowfield The Task Persistent Data Simulation The Mail Truck's Route Pathfinding Exercises Measuring a Robot Robot Efficiency Persistent Group Chapter 8: Bugs and Errors Language Strict Mode Types Testing Debugging Error Propagation Exceptions Cleaning Up After Exceptions Selective Catching Assertions Summary Exercises Retry The Locked Box Chapter 9: Regular Expressions Creating a Regular Expression Testing for Matches Sets of Characters Repeating Parts of a Pattern Grouping Subexpressions Matches and Groups The Date Class Word and String Boundaries Choice Patterns The Mechanics of Matching Backtracking The replace Method Greed Dynamically Creating RegExp Objects The search Method The lastIndex Property Looping Over Matches Parsing an INI File International Characters Summary Exercises Regexp Golf Quoting Style Numbers Again Chapter 10: Modules Modules as Building Blocks Packages Improvised Modules Evaluating Data as Code CommonJS ECMAScript Modules Building and Bundling Module Design Summary Exercises A Modular Robot Roads Module Circular Dependencies Chapter 11: Asynchronous Programming Asynchronicity Crow Tech Callbacks Promises Failure Networks Are Hard Collections of Promises Network Flooding Message Routing Async Functions Generators The Event Loop Asynchronous Bugs Summary Exercises Tracking the Scalpel Building Promise.all Chapter 12: Project: A Programming Language Parsing The Evaluator Special Forms The Environment Functions Compilation Cheating Exercises Arrays Closure Comments Fixing Scope Part II: Browser Chapter 13: JavaScript and the Browser Networks and the Internet The Web HTML HTML and JavaScript In the Sandbox Compatibility and the Browser Wars Chapter 14: The Document Object Model Document Structure Trees The Standard Moving Through the Tree Finding Elements Changing the Document Creating Nodes Attributes Layout Styling Cascading Styles Query Selectors Positioning and Animating Summary Exercises Build a Table Elements by Tag Name The Cat's Hat Chapter 15: Handling Events Event Handlers Events and DOM Nodes Event Objects Propagation Default Actions Key Events Pointer Events Mouse Clicks Mouse Motion Touch Events Scroll Events Focus Events Load Event Events and the Event Loop Timers Debouncing Summary Exercises Balloon Mouse Trail Tabs Chapter 16: Project: A Platform Game The Game The Technology Levels Reading a Level Actors Encapsulation as a Burden Drawing Motion and Collision Actor Updates Tracking Keys Running the Game Exercises Game Over Pausing the Game A Monster Chapter 17: Drawing on Canvas SVG The Canvas Element Lines and Surfaces Paths Curves Drawing a Pie Chart Text Images Transformation Storing and Clearing Transformations Back to the Game Choosing a Graphics Interface Summary Exercises Shapes The Pie Chart A Bouncing Ball Precomputed Mirroring Chapter 18: HTTP and Forms The Protocol Browsers and HTTP Fetch HTTP Sandboxing Appreciating HTTP Security and HTTPS Form Fields Focus Disabled Fields The Form as a Whole Text Fields Checkboxes and Radio Buttons Select Fields File Fields Storing Data Client-Side Summary Exercises Content Negotiation A JavaScript Workbench Conway's Game of Life Chapter 19: Project: A Pixel Art Editor Components The State DOM Building The Canvas The Application Drawing Tools Saving and Loading Undo History Let's Draw Why Is This So Hard? Exercises Keyboard Bindings Efficient Drawing Circles Proper Lines Part III: Node Chapter 20: Node.js Background The node Command Modules Installing with NPM Package Files Versions The File System Module The HTTP Module Streams A File Server Summary Exercises Search Tool Directory Creation A Public Space on the Web Chapter 21: Project: Skill-Sharing Website Design Long Polling HTTP Interface The Server Routing Serving Files Talks as Resources Long Polling Support The Client HTML Actions Rendering Components Polling The Application Exercises Disk Persistence Comment Field Resets Chapter 22: JavaScript and Performance Staged Compilation Graph Layout Defining a Graph Force-Directed Layout Avoiding Work Profiling Function Inlining Creating Less Garbage Garbage Collection Dynamic Types Summary Exercises Pathfinding Timing Optimizing Exercise Hints Chapter 2: Program Structure Looping a Triangle FizzBuzz Chessboard Chapter 3: Functions Minimum Recursion Bean Counting Chapter 4: Data Structures: Objects and Arrays The Sum of a Range Reversing an Array A List Deep Comparison Chapter 5: Higher-Order Functions Everything Dominant Writing Direction Chapter 6: The Secret Life of Objects A Vector Type Groups Iterable Groups Borrowing a Method Chapter 7: Project: A Robot Measuring a Robot Robot Efficiency Persistent Group Chapter 8: Bugs and Errors Retry The Locked Box Chapter 9: Regular Expressions Quoting Style Numbers Again Chapter 10: Modules A Modular Robot Roads Module Circular Dependencies Chapter 11: Asynchronous Programming Tracking the Scalpel Building Promise.all Chapter 12: Project: A Programming Language Arrays Closure Comments Fixing Scope Chapter 14: The Document Object Model Build a Table Elements by Tag Name The Cat's Hat Chapter 15: Handling Events Balloon Mouse Trail Tabs Chapter 16: Project: A Platform Game Pausing the Game A Monster Chapter 17: Drawing on Canvas Shapes The Pie Chart A Bouncing Ball Precomputed Mirroring Chapter 18: HTTP and Forms Content Negotiation A JavaScript Workbench Conway's Game of Life Chapter 19: Project: A Pixel Art Editor Keyboard Bindings Efficient Drawing Circles Proper Lines Chapter 20: Node.js Search Tool Directory Creation A Public Space on the Web Chapter 21: Project: Skill-Sharing Website Disk Persistence Comment Field Resets Chapter 22: JavaScript and Performance Pathfinding Optimizing Index Completely revised and updated, this best-selling introduction to programming in JavaScript focuses on writing real applications. JavaScript lies at the heart of almost every modern web application, from social apps like Twitter to browser-based game frameworks like Phaser and Babylon. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications. This much anticipated and thoroughly revised third edition of Eloquent JavaScript dives deep into the JavaScript language to show you how to write beautiful, effective code. It has been updated to reflect the current state of Java¬Script and web browsers and includes brand-new material on features like class notation, arrow functions, iterators, async functions, template strings, and block scope. A host of new exercises have also been added to test your skills and keep you on track. As with previous editions, Haverbeke continues to teach through extensive examples and immerses you in code from the start, while exercises and full-chapter projects give you hands-on experience with writing your own programs. You start by learning the basic structure of the JavaScript language as well as control structures, functions, and data structures to help you write basic programs. Then you'll learn about error handling and bug fixing, modularity, and asynchronous programming before moving on to web browsers and how JavaScript is used to program them. As you build projects such as an artificial life simulation, a simple programming language, and a paint program, you'll learn how to: - Understand the essential elements of programming, including syntax, control, and data - Organize and clarify your code with object-oriented and functional programming techniques - Script the browser and make basic web applications - Use the DOM effectively to interact with browsers - Harness Node.js to build servers and utilities Isn't it time you became fluent in the language of the Web? \* All source code is available online in an inter¬active sandbox, where you can edit the code, run it, and see its output instantly.
"A concise and balanced mix of principles and pragmatics. I loved the tutorial-style game-like program development. This book rekindled my earliest joys of programming. Plus, JavaScript!" —Brendan Eich, creator of JavaScript
JavaScript is the language of the Web, and it's at the heart of every modern website from the lowliest personal blog to the mighty Google Apps. Though it's simple for beginners to pick up and play with, JavaScript is not a toy—it's a flexible and complex language, capable of much more than the showy tricks most programmers use it for.
Eloquent JavaScript goes beyond the cut-and-paste scripts of the recipe books and teaches you to write code that's elegant and effective. You'll start with the basics of programming, and learn to use variables, control structures, functions, and data structures. Then you'll dive into the real JavaScript artistry: higher-order functions, closures, and object-oriented programming.
Along the way you'll learn to:
- Master basic programming techniques and best practices
- Harness the power of functional and object-oriented programming
- Use regular expressions to quickly parse and manipulate strings
- Gracefully deal with errors and browser incompatibilities
- Handle browser events and alter the DOM structure
Most importantly, Eloquent JavaScript will teach you to express yourself in code with precision and beauty. After all, great programming is an art, not a science—so why settle for a killer app when you can create a masterpiece?
JavaScript is at the heart of almost every modern Web application, whether it's Google Apps, Twitter, or the newest browser-based game. Though it's simple for beginners to pick up and play with, JavaScript is not a toy-it's a flexible and complex language that can be used to build full-scale applications. Eloquent JavaScript dives into this flourishing language and teaches you to write code that's beautiful and effective. By immersing you in example code and encouraging experimentation right from the start, the author quickly gives you the tools you need to build your own programs. As you follow along with examples like an artificial life simulation and a version of the classic game Sokoban, you'll learn to: -Understand the essential elements of programming: syntax, control, and data -Use object-oriented and functional programming techniques to organize and clarify your programs -Script the browser and make basic Web applications -Work with tools like regular expressions and XMLHttpRequest objects And since programming is an art that's best learned by doing, all example code is available online in an interactive sandbox for you to experiment with. With Eloquent JavaScript as your guide, you can tweak, expand, and modify the author's code, or throw it away and build your own creations from scratch. Before you know it, you'll be fluent in the language of the Web Completely Revised And Updated, This Best-selling Introduction To Programming In Javascript Focuses On Writing Real Applications. Eloquent Javascript Dives Into The Javascript Language To Show Programmers How To Write Elegant, Effective Javascript Code. Like Any Good Programming Book, Eloquent Javascript Begins With Fundamentals--variables, Control Structures, Functions, And Data Structures--then Moves On To Complex Topics Like Object-oriented Programming And Regular Expressions. This Third Edition Introduces New Features Covering The 2017 Version Of Javascript, Such As Class Notation, Arrow Functions, Iterators, Async Functions, Template Strings, And Black Scope. Author Marijn Haverbeke Keeps The Friendly Tone And Easy-to-follow Explanations That Made The Original A Hit, And He Adds New Exercises For Readers To Test Their Skills. Eloquent Javascript Will Have Readers Fluent In The Language Of The Web In No Time, Eloquently. Introduction Basic values, variables, and control flow Functions Data structures: Objects and Arrays Error Handling Functional Programming Searching Object-oriented Programming Modularity Regular Expressions Web programming: A crash course The Document-Object Model Browser Events HTTP requests