JS ES5 / 2009
In this page:
"use strict";
array.forEach(fn);
JSON.parse(text);
Object.keys(object);
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.
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.
Example: Native JSON Support
// Declare the constant `obj`, set to `JSON.parse('{"name": "Sam"}')`
// Declare the constant `obj`, set to `JSON.parse('{"name": "Sam"}')`
const obj = JSON.parse('{"name": "Sam"}');
// Print `obj.name` to the console
// Print `obj.name` to the console
console.log(obj.name);
// Print `JSON.stringify(obj)` to the console
// Print `JSON.stringify(obj)` to the console
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.
Example: Array Iteration Methods
[1, 2, 3].forEach(n => console.log(n));
// Print `[1, 2, 3].map(n => n * 2)` to the console
// Print `[1, 2, 3].map(n => n * 2)` to the console
console.log([1, 2, 3].map(n => n * 2));
// Print `[1, 2, 3].filter(n => n > 1)` to the console
// Print `[1, 2, 3].filter(n => n > 1)` to the console
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.
Example: Property Getters and Setters
// Declare the constant `obj`; its value is built below
// Declare the constant `obj`; its value is built below
const obj = {
_value: 10,
get value() { return this._value; },
set value(v) { this._value = v; },
};
// Assign `20` to `obj.value`
// Assign `20` to `obj.value`
obj.value = 20;
// Print `obj.value` to the console
// Print `obj.value` to the console
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.
Example: Why ES5 Still Matters Today
// Declare the variable `nums`, set to `[1, 2, 3]`
// Declare the variable `nums`, set to `[1, 2, 3]`
var nums = [1, 2, 3];
// Declare the variable `doubled`, set to `nums.map(function (n) { return n * 2; })`
// Declare the variable `doubled`, set to `nums.map(function (n) { return n * 2; })`
var doubled = nums.map(function (n) { return n * 2; });
// Print `doubled` to the console
// Print `doubled` to the console
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: