JavaScript 2009 (ES5)
ECMAScript 2009
The first major revision to JavaScript.
ECMAScript 2009 is also known as ES5.
ES5 Features
| Feature | Description |
|---|---|
| "use strict" | Allows code to be executed in "strict mode" |
| String [] access | Returns the character at a specified index in a string |
| Multiline strings | Aallows strings over multiple lines if escaped with \ |
| String.trim() | Removes whitespace from both sides of a string |
| Array.isArray() | Returns true if a variable is an array |
| Array forEach() | Calls a function for each array element |
| Array map() | Creates a new array from a function on each element |
| Array filter() | Creates an array from array elements that passes a test |
| Array reduce() | Reduces an array to a single value (from left) |
| Array reduceRight() | Reduces an array to a single value (from right) |
| Array every() | Checks if all array values pass a test |
| Array some() | Checks if some values pass a test |
| Array indexOf() | Search for an element value and returns its position |
| Array lastIndexOf() | Search for an element value and returns its position |
| JSON.parse() | Convert JSON into a JavaScript object |
| JSON.stringify() | Convert JSON into a string |
| Date.now() | Returns the number of milliseconds since zero date |
| Date toISOString() | Converts a date object into to an ISO string |
| Date toJSON() | Converts a date object into to a JSON string |
| Property getters | Allows for defining how a property value is retrieved |
| Property setters | Allows for defining how a property value is set |
| Reserved names | Allows reserved names as property names |
| Object.create() | Creates an object from an existing object |
| Object.keys() | Returns an array with the keys of an object |
| Object management | Object management methods |
| Object protection | Object protection methods |
| Object defineProperty() | Allows for defining or changing object properties |
| Function bind() | Let objects borrow methods from other objects |
| Trailing commas | allows trailing commas in object and array definitions: |
Browser Support
JavaScript 2009 is supported in all modern browsers sinceJuly 2013:
| Chrome 23 | IE/Edge 10 | Firefox 21 | Safari 6 | Opera 15 |
| Sep 2012 | Sep 2012 | Apr 2013 | Jul 2012 | Jul 2013 |
The "use strict" Directive
"use strict" defines that the JavaScript code should be executed in "strict mode".
With strict mode you can, for example, not use undeclared variables.
You can use strict mode in all your programs. It helps you to write cleaner code,like preventing you from using undeclared variables.
"use strict" is just a string expression. Old browsers will not throw an error if they don't understand it.
Property Access on Strings
ThecharAt() method returns the character at a specified index (position) in a string:
ES5 allows property access on strings:
Property access on string might be a little unpredictable.
Read more inJS String Methods.
Strings Over Multiple Lines
ES5 allows string literals over multiple lines if escaped with a backslash:Note
The \ method might not have universal support.
Older browsers might treat the spaces around the backslash differently.
Some older browsers do not allow spaces behind the \ character.
A safer way to break up a string literal, is to use string addition:
Reserved Words as Property Names
ES5 allows reserved words as property names:
String trim()
Thetrim() method removes whitespace from both sides of a string.
Learn more inJS String Methods.
Array.isArray()
TheisArray() method checks whether an object is an array.
Example
result = Array.isArray(fruits);
Learn more inJS Array Methods.
Array forEach()
TheforEach() method calls a function once for each array element.
Learn more inJS Array Iteration Methods.
Array map()
Themap() method creates a new array by performing a function on each array element.
Example
Multiply each array value by 2:
const numbers2 = numbers1.map(myFunction);
function myFunction(value) {
return value * 2;
}
Learn more inJS Array Iteration Methods.
Array filter()
Thefilter() method creates a new array from array elements that passes a test.
Example
Create a new array from elements with a value larger than 18:
const over18 = numbers.filter(myFunction);
function myFunction(value) {
return value > 18;
}
Learn more inJS Array Iteration Methods.
Array reduce()
Thereduce() method reduces an array to a single value.
Example
Find the sum of all numbers in an array:
let sum = numbers.reduce(myFunction);
function myFunction(total, value) {
return total + value;
}
Learn more inJS Array Iteration Methods.
Array reduceRight()
ThereduceRight() method reduces an array to a single value (from right to left).
Example
Find the sum of all numbers in an array:
let sum = numbers1.reduceRight(myFunction);
function myFunction(total, value) {
return total + value;
}
Learn more inJS Array Iteration Methods.
Array every()
Theevery() method checks if all array values pass a test.
Example
Check if all values are over 18:
let allOver18 = numbers.every(myFunction);
function myFunction(value) {
return value > 18;
}
Learn more inJS Array Iteration Methods.
Array some()
Thesome() method checks if some array values pass a test.
Example
Check if some values are over 18:
let allOver18 = numbers.some(myFunction);
function myFunction(value) {
return value > 18;
}
Learn more inJS Array Iteration Methods.
Array indexOf()
TheindexOf() method searches for an element value and returns its position.
Example
Search an array for an element value:
let position = fruits.indexOf("Apple") + 1;
Learn more inJS Array Search Methods.
Array lastIndexOf()
lastIndexOf() is the same asindexOf(), but searches from the end of the array.
Example
let position = fruits.lastIndexOf("Apple") + 1;
Learn more inJS Array Search Methods.
JSON.parse()
A common use of JSON is to receive data from a web server.
Imagine you received this text string from a web server:
The JavaScript functionJSON.parse() is used to convert the text into a JavaScript object:
Example
const myObj = JSON.parse(txt);
Read more in ourJSON Tutorial.
JSON.stringify()
A common use of JSON is to send data to a web server.
When sending data to a web server, the data has to be a string.
Imagine we have this object in #"tryit.asp?filename=tryjson_stringify">Try it Yourself »

