JS ES5 / 2009
In this page:
Strict Mode
"use strict" at the top of a script or function opts into a stricter set of JavaScript rules -- catching mistakes like accidentally creating a global variable by forgetting a declaration keyword, and disallowing some confusing or unsafe older patterns.
Note: Modern module-based code (type="module", or code processed by a bundler) is automatically strict, but plain, non-module scripts need "use strict" explicitly to get these benefits.
Warning: Strict mode changes behavior in ways that can surface previously silent bugs -- code that "worked" without it might throw a new error once strict mode is enabled, which is exactly the intended benefit.
Example: Strict Mode
"use strict";
try {
x = 5; // undeclared, throws instead of creating a global
} catch (e) {
console.log(e.message);
}
Native JSON Support
JSON.parse() converts a JSON-formatted string into a JavaScript value, and JSON.stringify() converts a JavaScript value into a JSON string -- before ES5 standardized these, developers relied on separate libraries or the risky eval() function to work with JSON data.
Note: Use JSON.parse() and JSON.stringify() as the standard, built-in way to convert between JSON text and JavaScript values -- no external library needed.
Warning: JSON.parse() on a malformed JSON string throws an error rather than returning something usable -- wrap it in a try/catch when parsing data from an untrusted or unpredictable source.
Example: Native JSON Support
const obj = JSON.parse('{"name": "Sam"}');
console.log(obj.name);
console.log(JSON.stringify(obj));
Array Iteration Methods
forEach(), map(), filter(), and reduce() -- all ES5 additions -- gave arrays built-in, functional-style iteration methods, replacing manual for-loop-based iteration for many common tasks and remaining some of the most frequently used JavaScript methods to this day.
Note: Reach for map/filter/reduce over a manual for loop whenever the goal is transforming, filtering, or combining an array's values -- these methods communicate intent more clearly.
Warning: forEach() does not return a new array the way map() does -- calling forEach() expecting a transformed result back is a common beginner mix-up between the two.
Example: Array Iteration Methods
[1, 2, 3].forEach(n => console.log(n));
console.log([1, 2, 3].map(n => n * 2));
console.log([1, 2, 3].filter(n => n > 1));
Property Getters and Setters
ES5 introduced getter and setter syntax within object literals -- get propertyName() { } and set propertyName(value) { } -- letting a property's read or write behavior run custom code, like computing a derived value or validating an assignment, while still looking like a plain property from the outside.
Note: Use a getter for a computed value derived from other properties, so callers can read it like a plain property without needing to call a method.
Warning: A setter that fails to actually store the assigned value (or store it correctly) can create confusing bugs where an assignment appears to succeed but silently has no effect.
Example: Property Getters and Setters
const obj = {
_value: 10,
get value() { return this._value; },
set value(v) { this._value = v; },
};
obj.value = 20;
console.log(obj.value);
Why ES5 Still Matters Today
Because ES5 support is effectively universal across every browser still in use, code relying only on ES5 features (rather than ES6+) is the safest possible baseline for maximum compatibility -- and understanding ES5 helps when reading older tutorials, libraries, and codebases still written in that style.
Note: When maximum compatibility with very old browsers is a genuine requirement, writing in ES5 style (or transpiling ES6+ code down to it) remains a valid, safe choice.
Warning: For nearly all modern web development targeting current browsers, restricting yourself to ES5-only syntax is an unnecessary limitation -- ES6+ features are safe to use in the vast majority of real-world projects today.
Example: Why ES5 Still Matters Today
var nums = [1, 2, 3];
var doubled = nums.map(function (n) { return n * 2; });
console.log(doubled);
- Assuming ES5-era code (using var, function expressions) is somehow "wrong" or outdated to the point of being unusable -- it remains perfectly valid, supported JavaScript, just an older style.
- Forgetting to enable strict mode explicitly with "use strict" -- unlike later module syntax, ES5 strict mode is opt-in, not automatic.
- Confusing ES5's array methods (forEach, map, filter) with newer ES6+ additions -- these particular ones actually originate from ES5, predating arrow functions and classes.
- "use strict" enables strict mode, catching common mistakes and disallowing some error-prone older JavaScript patterns.
- JSON.parse() and JSON.stringify() were standardized in ES5, providing native JSON support without needing a separate library.
- Array methods like forEach, map, filter, and reduce were introduced in ES5, and remain some of the most commonly used methods in JavaScript today.
ES5 has been supported in effectively every browser in active use for well over a decade, making it the safest baseline to assume support for.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: