Eloquent JavaScript
Marijn Haverbeke
Copyright 2011
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the authors nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
No Starch Press
Introduction
Back in the 1970s, when personal computers were first introduced, most of them came equipped with a simple programming languageusually a variant of BASICand interaction with the computer required use of this language. This meant that, for those of us to whom technological tinkering came naturally, going from simple computer use to programming was easy.
These days, with computers being many times more powerful and cheaper than in the 1970s, software interfaces tend to present a slick graphics interface manipulated with the mouse, rather than a language. This has made computers much more accessible and, on the whole, is a big improvement. However, it has also put up a barrier between the computer user and the world of programminghobbyists have to actively find themselves a programming environment rather than having one available as soon as the computer starts.
Under the covers, our computer systems are still pervaded by various programming languages. Most of these languages are much more advanced than the BASIC dialects in those early personal computers. For example, the JavaScript language, the subject of this book, exists in every mainstream web browser.
On Programming
I do not enlighten those who are not eager to learn, nor arouse those who are not anxious to give an explanation themselves. If I have presented one corner of the square and they cannot come back to me with the other three, I should not go over the points again. |
-- Confucius |
Besides explaining JavaScript, this book tries to be an introduction to the basic principles of programming. Programming, it turns out, is hard. The fundamental rules are typically simple and clearbut programs, while built on top of these basic rules, tend to become complex enough to introduce their own rules and complexity. Because of this, programming is rarely simple or predictable. As Donald Knuth, who is something of a founding father of the field, says, it is an art rather than a science.
To get something out of this book, more than just passive reading is required. Try to stay sharp, make an effort to understand the example code, and only continue when you are reasonably sure you understand the material that came before.
The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs. |
-- Joseph Weizenbaum, Computer Power and Human Reason |
A program is many things. It is a piece of text typed by a programmer, it is the directing force that makes the computer do what it does, it is data in the computers memory, yet it controls the actions performed on this same memory. Analogies that try to compare programs to objects we are familiar with tend to fall short, but a superficially fitting one is that of a machine. The gears of a mechanical watch fit together ingeniously, and if the watchmaker was any good, it will accurately show the time for many years. The elements of a program fit together in a similar way, and if programmers know what they are doing, their program will run without crashing.
A computer is a machine built to act as a host for these immaterial machines. Computers themselves can only do stupidly straightforward things. The reason they are so useful is that they do these things at an incredibly high speed. A program can ingeniously combine enormous numbers of these simple actions in order to do very complicated things.
To some of us, writing computer programs is a fascinating game. A program is a building of thought. It is costless to build, it is weightless, and it grows easily under our typing hands. If we are not careful, its size and complexity will grow out of control, confusing even the person who created it. This is the main problem of programming: keeping programs under control. When a program works, it is beautiful. The art of programming is the skill of controlling complexity. The great program is subdued, made simple in its complexity.
Today, many programmers believe that this complexity is best managed by using only a small set of well-understood techniques in their programs. They have composed strict rules ( best practices ) about the form programs should have, and the more zealous among them will denounce those who break these rules as bad programmers.
What hostility to the richness of programmingto try to reduce it to something straightforward and predictable and to place a taboo on all the weird and beautiful programs! The landscape of programming techniques is enormous, fascinating in its diversity, and still largely unexplored. It is certainly littered with traps and snares, luring the inexperienced programmer into all kinds of horrible mistakes, but that only means you should proceed with caution and keep your wits about you. As you learn, there will always be new challenges and new territory to explore. Programmers who refuse to keep exploring will surely stagnate, forget their joy, and lose the will to program (and become managers).
Why Language Matters
In the beginning, at the birth of computing, there were no programming languages. Programs looked something like this:
00110001 00000000 0000000000110001 00000001 0000000100110011 00000001 0000001001010001 00001011 0000001000100010 00000010 0000100001000011 00000001 0000000001000001 00000001 0000000100010000 00000010 0000000001100010 00000000 00000000
That is a program to add the numbers from 1 to 10 together and print out the result (1 + 2 + ... + 10 = 55)
. It could run on a very simple, hypothetical machine. To program early computers, it was necessary to set large arrays of switches in the right position, or punch holes in strips of cardboard and feed them to the computer. You can imagine how this was a tedious, error prone procedure. Even the writing of simple programs required much cleverness and discipline, and complex ones were nearly inconceivable.
Of course, manually entering these arcane patterns of bits (which is what the ones and zeros shown previously are generally called) did give the programmer a profound sense of being a mighty wizard. And that has to be worth something in terms of job satisfaction.
Each line of the program contains a single instruction. It could be written in English like this:
Store the number 0 in memory location 0.