Acknowledgements
The JavaScript language was created by Brendan Eich at Netscape Communications. The first version was created in an astonishing 10 days in May 1995 and remains an enduring monument to his skills.
Steve Jobs and Steve Wozniak for their significant contributions to the Atari game “Breakout” which Steve Wozniak subsequently implemented in software on the Apple II microcomputer.
Nintendo for “Donkey Kong” as this was arguably the first true platform game.
Dong Nguyen for “Flappy”.
Silas Warner working at Muse Software for Castle Wolfenstein.
Preface to first edition
Why choose JavaScript as your first computer programming language? If you are going to learn to program then you have to pick a start point and a first language. It probably does not matter which one you pick but there is a strong case to be made for picking one that is flexible and powerful and that includes the full range of modern language concepts while also allowing you to experiment with features that push the boundaries a bit.
JavaScript is everywhere on the Internet and is the tool of a host of programming professionals. If it is your end goal to become a web developer then JavaScript is the only place to start. If your ambition is to become a games developer then you probably have a different language as a long-term goal but in the meantime, JavaScript will do very nicely to introduce all of the key concepts and to give you some crucial experience. If you are looking towards more general computer science skills then a fast readily available programming tool like JavaScript is just what you need. Even if you are just looking to find out what programming is all about then again JavaScript is a good choice as you can achieve some astonishing results in return for a limited time investment.
At its simplest you can use a web browser to provide all of the technical resources required to execute your JavaScript program code quickly and securely. Web browsers also gift you a straightforward set of tools for managing interaction with your users. Web based graphics can be awesome as we will see working through this book. It is difficult to understate the advantages stemming from the availability of a rich User Interface (UI) with a mouse keyboard, game controller or “touch” readily available to your programs.
There is an authoritative book written by the great Douglas Crockford called “JavaScript: The Good Parts”. That title implies that the language includes some bad parts. To quote, “A beautiful, elegant, lightweight and highly expressive language lies buried under a steaming pile of good intentions and blunders”. I think that is more than a bit harsh. True, there are a (very) few bits best avoided and we will certainly try to avoid them in this book - except perhaps to point out some outmoded and slightly risky features that you might come across elsewhere.
You can tackle all of the projects in this book using the simplest of tools - something like notepad.exe on Windows based PCs. However, this book does encourage you to trade up and take advantage of some of the high-quality free tools available. Using professional tools like Visual Studio Code will add to the learning experience while carrying some of the workload. It is worthwhile taking a few minutes before getting stuck into the code and building a game to set up your work environment properly by installing the suggested range of development tools.
Preface to the second edition
Why a second edition at all? Things move on. In the context of this book, the tools improved and that simplified some aspects of setting up the recommended development environment.
This edition features more illustrations. When the book was first being written and the code run and tested at every step the illustrations were always there on a PC screen. This works well for anyone actively building a project – all the visual feedback is right there on their PC. This works less well for someone reviewing a project in the book prior to working through it. There was no opportunity to preview how things will develop. Hopefully, this edition goes some way to addressing that original omission.
A second edition is also a great opportunity to revise and improve the text and fix a few typos and some clumsy phraseology.
One or two minor items that were featured in the first edition have now become a little outdated and so have been moved to the website where they might still be useful to some readers.
Please remember that almost all programming languages and programming tool sets change over time. Programmers are forever tinkering with and improving their tools. If you hit something that does not work then please visit the book web site and check for any advice. If there is nothing that seems to address your problem then use the web site to get in touch, so that any changes and amendments to the advice and instructions can be made available to all readers.
Introduction
Just a couple of notes on how program code and other items are presented in this book.
There is an assumption that readers know how to type text into a window, navigate around that text and edit the text. While many professional programmers have pretty advanced typing skills a little experience doing things like writing emails should suffice for the projects in this book.
This book is intended to be enjoyed by people with a range of objectives and learning styles. Some might want to dash on to the game code and get things running as quickly as possible, hopefully returning to fill in technical details missed at a later stage. Others might want to take a more measured approach, looking to learn and understand each new concept or language feature in some detail. The main text should suit both approaches while shaded sections provide additional technical background.
Program code presented in boxed areas using a monospaced font is intended to be the code to be typed in to develop each program step or to test a newly introduced language feature.
The code used in this book can be downloaded from the book web site but the reader is encouraged to type it in as they follow through the development process in each chapter. Long experience has convinced everyone I have ever met that this approach is the only one that truly works. As your skills develop, you will find yourself able to read well written program code as if it was descriptive text but even at that stage you will want to tweak and run your own version before you feel that you understand it thoroughly.
Most images in the book are reproduced in full colour on the book website together with additional notes, extra code samples and further reading on related topics. Do please visit and leave feedback.
I hope that you enjoy working through this book. Programming is a very pleasurable activity and programming even simple games adds to the fun.
Contents
Code downloads and chapter images are available here.