JavaScript ES5

ECMAScript 2009, also known as ES5, was the first major revision to JavaScript.

This chapter describes the most important features of ES5.

ES5 Features

  • "use strict"
  • String[number] access
  • Multiline strings
  • String.trim()
  • Array.isArray()
  • Array forEach()
  • Array map()
  • Array filter()
  • Array reduce()
  • Array reduceRight()
  • Array every()
  • Array some()
  • Array indexOf()
  • Array lastIndexOf()
  • JSON.parse()
  • JSON.stringify()
  • Date.now()
  • Date toISOString()
  • Date toJSON()
  • Property getters and setters
  • Reserved words as property names
  • Object methods
  • Object defineProperty()
  • Function bind()
  • Trailing commas

Browser Support

ES5 (JavaScript 2009) fully supported in all modern browsers since July 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.

Read more in JS Strict Mode.


Property Access on Strings

The charAt() method returns the character at a specified index (position) in a string:

Example

var str = "HELLO WORLD";
str.charAt(
0);            // returns H

- »

ES5 allows property access on strings:

Example

var str = "HELLO WORLD";
str[
0];                   // returns H

- »

Property access on string might be a little unpredictable.

Read more in JS String Methods.


Strings Over Multiple Lines

ES5 allows string literals over multiple lines if escaped with a backslash:

Example

"Hello
Dolly!"
;

- »

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:

Example

"Hello " +
"Dolly!";

- »


Reserved Words as Property Names

ES5 allows reserved words as property names:

Object Example

var obj = {name: "John"new"yes"}

- »


String trim()

The trim() method removes whitespace from both sides of a string.

Example

var str = "       Hello World!        ";
alert(str.trim());

- »

Read more in JS String Methods.



Array.isArray()

The isArray() method checks whether an object is an array.

Example

function myFunction() {
  
var fruits = ["Banana""Orange""Apple""Mango"];
  
var x = document.getElementById("demo");
  x.innerHTML = Array.isArray(fruits);
}

- »

Read more in JS Arrays.


Array forEach()

The forEach() method calls a function once for each array element.

Example

var txt = "";
var numbers = [45491625];
numbers.forEach(myFunction);

function myFunction(value) {
  txt = txt + value + 
"<br>";
}

- »

Learn more in JS Array Iteration Methods.


Array map()

This example multiplies each array value by 2:

Example

var numbers1 = [45491625];
var numbers2 = numbers1.map(myFunction);

Login
ADS CODE