← Nonfiction

JavaScript: The Good Parts

by Douglas Crockford · 2008

Most programming languages contain good and bad parts, but JavaScript has more than its share of the bad, having been developed and released in a hurry before it could be refined. This authoritative book scrapes away these… more

29 quotes ★ 4.23 (8,675) Nonfiction

Quotes from the Book

Computer programs are the most complex things that humans make.
13
We see a lot of feature-driven product design in which the cost of features is not properly accounted. Features can have a negative value to customers because they make the products more difficult to understand and use. We are finding that people like products that just work. It turns out that designs that just work are much harder to produce that designs that assemble long lists of features.
12
Generally, the craft of programming is the factoring of a set of requirements into a a set of functions and data structures.
8
Obsolete comments are worse than no comments.
3
If you want to learn more about the bad parts and how to use them badly, consult any other JavaScript book.
3
If we offend, it is with our good will That you should think, we come not to offend, But with good will. To show our simple skill, That is the true beginning of our end.
2
Quality was not a motivating concern in the design, implementation, or standardization of JavaScript. That puts a greater burden on the users of the language to resist the language's weaknesses.
Fortunately, JavaScript has some extraordinarily good parts. In JavaScript, there is a beautiful, elegant, highly expressive language that is buried under a steaming pile of good intentions and blunders.
JavaScript, being a loosely typed language, never casts. The lineage of an object is irrelevant. What matters about an object is what it can do, not what it is descended from.
Some languages offer the tail recursion optimization. This means that if a function returns the result of invoking itself recursively, then the invocation is replaced with a loop, which can significantly speed things up. Unfortunately, JavaScript does not currently provide tail recursion optimization. Functions that recurse very deeply can fail by exhausting the return stack:
There are four patterns of invocation in JavaScript: the method invocation pattern, the function invocation pattern, the constructor invocation pattern, and the apply invocation pattern.
Objects are passed around by reference. They are never copied:
Attempting to retrieve values from undefined will throw a TypeError exception. This can be guarded against with the && operator: flight.equipment                              // undefined flight.equipment.model                        // throw "TypeError" flight.equipment && flight.equipment.model    // undefined
The || operator can be used to fill in default values: var middle = stooge["middle-name"] || "(none)"; var status = flight.status || "unknown";
A property value can be any JavaScript value except for undefined.
If a return expression is not specified, then the return value will be undefined.
a return expression is not specified, then the return value will be undefined.
The try statement executes a block and catches any exceptions that were thrown by the block. The catch clause defines a new variable that will receive the exception object. The throw statement raises an exception. If the throw statement is in a try block, then control goes to the catch clause. Otherwise, the function invocation is abandoned, and control goes to the catch clause of the try in the calling function. The expression is usually an object literal containing a name property and a message property. The catcher of the exception can use that information to determine what to do.
The other form (called for in) enumerates the property names (or keys) of an object. On each iteration, another property name string from the object is assigned to the variable.
Here are the falsy values: false null undefined The empty string '' The number 0 The number NaN All other values are truthy, including true, the string 'false', and all objects.
The switch, while, for, and do statements are allowed to have an optional label prefix that interacts with the break statement.
When used inside of a function, the var statement defines the function’s private variables.
Numbers JavaScript has a single number type. Internally, it is represented as 64-bit floating point, the same as Java’s double. Unlike most other programming languages, there is no separate integer type, so 1 and 1.0 are the same value. This is a significant convenience because problems of overflow in short integers are completely avoided, and all you need to know about a number is that it is a number. A large class of numeric type errors is avoided.
Names A name is a letter optionally followed by one or more letters, digits, or underbars. A name cannot be one of these reserved words: abstract boolean break byte case catch char class const continue debugger default delete do double else enum export extends false final finally float for function goto if implements import in instanceof int interface long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var volatile void while with Most of the reserved words in this list are not used in the language. The list does not include some words that should have been reserved but were not, such as undefined, NaN, and Infinity. It is not permitted to name a variable or parameter with a reserved word. Worse, it is not permitted to use a reserved word as the name of an object property in an object literal or following a dot in a refinement. Names are used for statements, variables, parameters, property names, operators, and labels.
The best thing about JavaScript is its implementation of functions. It got almost everything right. But, as you should expect with JavaScript, it didn’t get everything right.
suppress empty strings in the output array when the separator is a regular expression: var f = '|a|b|c|'.split(/\|/); // f is ['a', 'b', 'c'] on some systems, and // f is ['', 'a', 'b', 'c', ''] on others string.substring(start, end ) The substring method is the same as the slice method except that it doesn’t handle the adjustment for negative parameters. There is no reason to use the substring method. Use slice instead. string.toLocaleLowerCase( ) The toLocaleLowerCase method produces a new string that is made by converting this string to lowercase using the rules for the locale. This is primarily for the benefit of Turkish because in that language `I’ converts to 1
Few classical programmers found prototypal inheritance to be acceptable, and classically inspired syntax obscures the language’s true prototypal nature. It is the worst of both worlds.
JavaScript is a language with more than its share of bad parts.
undefined and NaN are not constants. They are global variables, and you can change their values. That should not be possible, and yet it is. Don’t do it.

About Douglas Crockford

We have collected 29 quotes from 1 book by Douglas Crockford on BookQuoters.

More about the author →

About the book

Most programming languages contain good and bad parts, but JavaScript has more than its share of the bad, having been developed and released in a hurry before it could be refined. This authoritative book scrapes away these bad features to reveal a subset of JavaScript that's more reliable, readable, and maintainable than the language as a whole—a subset you can use to create truly extensible and efficient code. Considered the JavaScript expert by many people in the development community, author Douglas Crockford identifies the abundance of good ideas that make JavaScript an outstanding object-oriented programming language-ideas such as functions, loose typing, dynamic objects, and an expressive object literal notation. Unfortunately, these good ideas are mixed in with bad and downright awful ideas, like a programming model based on global variables. When Java applets failed, JavaScript b

Book details

First published
2008
Goodreads rating
4.23 (9k ratings)

Keep reading