This book is for anyone who wants to learn JavaScript or to start programming for the first time. The book is designed to be kid friendly, but it can serve as a first programming book for beginners of all ages.
With this book, youll build up your knowledge of JavaScript gradually, starting with JavaScripts simple data types, before moving onto complex types, control structures, and functions. After that youll learn how to write code that reacts when the user moves the mouse or presses a key on the keyboard. Finally, youll learn about the canvas
element, which lets you use JavaScript to draw and animate anything you can imagine!
Along the way, youll create a few games to stretch your programming skills and put what youve learned to good use.
How to Read This Book
First off, read it in order! That might sound like a silly thing to say, but lots of people want to jump straight into the fun stuff, like making games. But each chapter is meant to build on what was covered in earlier chapters, so if you begin at the beginning, youll have an easier time when you get to the games.
Programming languages are like spoken languages: you have to learn the grammar and the vocabulary, and this takes time. The only way to improve is by writing (and reading) a lot of code. As you write more and more JavaScript, youll find certain parts of the language become second nature, and eventually youll become a fluent writer of JavaScript.
As you read, I encourage you to type out and test the code examples throughout the book. If you dont fully understand whats going on, try making small changes to see what effect they have. If the changes dont have the effect you expected, see if you can find out why.
Above all, work through the Try It Out and sections. Typing out the code that appears in the book is a good first step, but youll understand programming at a deeper level when you start writing your own code. If you find a challenge interesting, then keep at it! Come up with your own challenges to build even more onto the programs youve written.
Youll find sample solutions to the programming challenges (as well as the code files for the games and other examples) at http://nostarch.com/javascriptforkids/ . Try looking at the solutions after youve solved a challenge, so you can compare your approach to mine. Or, if youre stuck, you can check the solution for hints. But remember that these are just sample solutions. There are many, many different ways to accomplish the same goal in JavaScript, so dont worry if you end up with a completely different solution from mine!
If you come across a word and you dont know what it means, check the glossary at the back of the book. The glossary contains definitions for many of the programming terms youll encounter in this book.
Whats in this Book?
gives you a quick introduction to JavaScript and gets you started writing JavaScript in Google Chrome.
introduces variables and the basic data types used by JavaScript: numbers, strings, and Booleans.
is all about arrays, which are used to hold lists of other pieces of data.
is about objects, which contain pairs of keys and values.
is an introduction to HTML, the language used to create web pages.
shows you how to gain more control over your code using if
statements, for
loops, and other control structures.
puts together everything youve learned so far to create a simple Hangman word-guessing game.
shows you how to write your own functions so you can group together and reuse blocks of code.
introduces jQuery, a tool that makes it easy to control web pages using JavaScript.
shows you how to use timeouts, intervals, and event handlers to make your code more interactive.
teaches a style of programming called object-oriented programming .
introduces the canvas
element, which allows you to draw graphics on a web page with JavaScript.
shows you how to control those canvas
animations with the keyboard.
In , youll program a complete Snake game, using everything you learned in the previous 15 chapters!
The Afterword gives you some ideas for how to learn even more about programming.