| name | js-safety |
| description | JS Good Parts |
Douglas Crockford JavaScript Philosophy
Applying Douglas Crockford's defensive JavaScript philosophy from "JavaScript: The Good Parts" and JSLint. The language has good parts and bad parts—use only the good parts.
Core Philosophy
The Good Parts Exist
JavaScript has more bad parts than good parts. Professional JavaScript means knowing which parts to avoid entirely.
"JavaScript is the only language people feel they don't need to learn before using."
Subset Is Sufficient
You don't need all of JavaScript. A disciplined subset produces better programs than the full language.
The Bad Parts (Avoid Entirely)
Global Variables
function bad() {
x = 5;
}
function good() {
const x = 5;
}
== vs ===
'' == '0'
0 == ''
0 == '0'
false == 'false'
false == '0'
if (value === expected) { ... }
with Statement
with (obj) {
a = b;
}
eval
eval(userInput);
setTimeout("doThing()", 100);
setTimeout(doThing, 100);
Bitwise Operators
if (a & b) { }
if (a | b) { }
The Good Parts (Use These)
Functions as First-Class Objects
const double = x => x * 2;
const numbers = [1, 2, 3].map(double);
function counter() {
let count = 0;
return {
increment: () => ++count,
get: () => count
};
}
Object Literals
const point = {
x: 10,
y: 20,
distance(other) {
return Math.sqrt(
Math.pow(this.x - other.x, 2) +
Math.pow(this.y - other.y, 2)
);
}
};
Array Methods
const adults = people
.filter(p => p.age >= 18)
.map(p => p.name)
.sort();
Module Pattern
const myModule = (function() {
let privateVar = 0;
function privateMethod() { }
return {
publicMethod() {
privateMethod();
return privateVar;
}
};
})();
Defensive Patterns
Fail Fast
function divide(a, b) {
if (typeof a !== 'number' || typeof b !== 'number') {
throw new TypeError('Arguments must be numbers');
}
if (b === 0) {
throw new RangeError('Cannot divide by zero');
}
return a / b;
}
Default Parameters
function greet(name = 'Guest') {
return `Hello, ${name}`;
}
Object.freeze for Constants
const CONFIG = Object.freeze({
API_URL: 'https://api.example.com',
TIMEOUT: 5000
});
JSLint Rules (Apply These)
- Declare variables at top of function
- One var/let/const per declaration
- Always use braces for blocks
- No fallthrough in switch
- Strict equality only (===, !==)
- No bitwise unless intentional
- No eval, with, or implied globals
When to Apply
| Scenario | Apply Crockford |
|---|
| Legacy JS codebase | Yes - identify bad parts |
| Code review | Yes - flag dangerous patterns |
| New utility library | Yes - defensive style |
| Modern React/Vue | Partially - frameworks have own conventions |
| TypeScript | Less critical - types catch many issues |
Source Material
- "JavaScript: The Good Parts" (2008)
- JSLint and its documentation
- Crockford on JavaScript (video series)
- JSON specification (Crockford invented JSON)