← Back to JavaScript Course | Chapter 12: Reference & Interview | Lesson 9 of 9

JS ES5 / 2009

ES5, released in 2009, was the first major update to JavaScript in a decade and standardized features that laid the groundwork for everything that followed -- strict mode, JSON support, and the array iteration methods (forEach, map, filter, reduce) that are still used constantly in modern code today.
Syntax
javascript
"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.

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.

उदाहरण: Strict Mode

javascript
"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.

उदाहरण: Native JSON Support

javascript
// 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.

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.

उदाहरण: Array Iteration Methods

javascript
[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.

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.

उदाहरण: Property Getters and Setters

javascript
// 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.

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.

उदाहरण: Why ES5 Still Matters Today

javascript
// 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);
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 9 topics to unlock

0/9 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.