You will be able to write and read JavaScript that runs in ES5 environments by using var, function expressions, prototypes, and ES5 array methods.
What it is
ES5 is the fifth edition of ECMAScript, the standard behind JavaScript. It is the baseline many older codebases still target because it works in older browsers and simple JavaScript engines without a build step. The mental model is: variables are function-scoped, functions are ordinary values, objects are built with constructors and prototypes, and modern syntax such as arrow functions, classes, and block-scoped variables is not available. Related terms include strict mode, IIFE, polyfill, transpilation, ES3, and ES6 or ES2015.
Why it matters
- It lets you maintain legacy applications that still run in older browsers.
- It helps you understand code generated by transpilers such as Babel.
- It is useful in embedded JavaScript environments where only ES5 is supported.
- It teaches core JavaScript ideas like function scope, callbacks, and prototypes.
- It reduces surprises when reading older libraries or interview examples.
Syntax or steps
The smallest useful ES5 pattern is strict mode, var, function expressions, and ES5 array methods.
- Start with
"use strict";to catch common mistakes. - Declare variables with
var. - Create functions with function expressions instead of arrow functions.
- Use prototypes for shared object behavior.
- Use ES5 methods such as
forEach,map,filter, andreduce.
Example
"use strict";
var users = [
{ name: "Ada", age: 36 },
{ name: "Ben", age: 17 },
{ name: "Cleo", age: 29 }
];
var adults = users.filter(function(user) {
return user.age >= 18;
}).map(function(user) {
return user.name;
});
console.log(adults);
Part by part: "use strict"; enables strict mode. var users creates an array of objects. filter returns only users whose age is 18 or older. map transforms each remaining user into their name. The final output is ["Ada", "Cleo"].
Common mistakes
- Using
let,const, or arrow functions in ES5 code. Fix: replace them withvarand function expressions. - Assuming an
ifblock creates a new scope. Fix: remember thatvaris function-scoped, not block-scoped. - Losing
thisinside callbacks. Fix: store it in a variable such asvar self = this;or usebind. - Calling ES6 methods like
Object.assignwithout support. Fix: use ES5-compatible code or add a polyfill.
When to use it
ES5 is best when compatibility matters more than modern syntax. ES6 or ES2015 is better when you can use a build step or target modern browsers.
| Use ES5 when | Use ES6 or ES2015 when |
|---|---|
| You must support older browsers or engines. | You can transpile or target modern environments. |
| You are reading legacy code or generated output. | You want cleaner syntax like classes and arrow functions. |
| You need a stable baseline without build tools. | You want modules, destructuring, and block scope. |
Practice
Guided exercise: write an ES5 function that filters even numbers from an array.
var numbers = [1, 2, 3, 4];
var evens = numbers.filter(function(n) {
return n % 2 === 0;
});
console.log(evens);
Expected output: [2, 4].
Challenge: write an ES5 function expression that returns the sum of squares of an array. Hint: use map to square each number, then reduce to add them together.
Quick check
Question: Why does var inside an if block not create a new scope?
Answer: Because var is function-scoped or global-scoped in ES5. Blocks such as if, for, and while do not create new variable scopes.
Summary
ES5 is a compatibility-focused JavaScript baseline built around var, function expressions, prototypes, and core array methods. Understanding it helps you read older code, avoid modern syntax mistakes, and reason clearly about scope and callbacks.