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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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);
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: