JavaScript ES5


ECMAScript 2009, also called ES5, marked a significant update for JavaScript, being its initial major revision.

This section explains the key aspects of ES5.

ES5 Features


The "use strict" Directive

The code ""use strict" tells JavaScript to run the code in a stricter mode.

Strict mode prevents the use of variables that haven't been declared, for instance.

You can apply strict mode to all your programs. This mode helps you write neater code by stopping you from using variables that haven't been declared.

The phrase ""use strict" is a simple text. Older web browsers won't show an error if they don't recognize it.


Property Access on Strings

The charAt() function gives you the character at a particular position in a string.

In ES5, you can access properties on strings.

Accessing information in a string could be somewhat unpredictable.

Learn more by visiting JS String Methods.


Strings Over Multiple Lines

In ES5, you can use a backslash to allow string literals to span multiple lines.

The \ method may not work everywhere.
Older browsers could handle spaces around the backslash in a different way.
Some older browsers may not permit spaces after the \ character.

A more secure method to split a string literal is by employing string addition.


Reserved Words as Property Names

ES5 permits using reserved words as names for properties.


String trim()

The trim() function gets rid of extra spaces at the beginning and end of a text.


Array.isArray()

The isArray() function verifies if an object is an array.


Array forEach()

The forEach() function is used to execute a specific function for every element in an array.


Array map()

This example doubles each value in an array by multiplying it by 2.


Array filter()

This example forms a fresh array by selecting elements that have a value greater than 18.


Array reduce()

This example calculates the total of all numbers in a list:


Array reduceRight()

This illustration calculates the total of numbers within an array.


Array every()

This sample verifies whether all the values are greater than 18.


Array some()

This sample verifies whether certain values are greater than 18.


Array indexOf()

Look for a specific value in an array and find its position.


Array lastIndexOf()

The lastIndexOf() function works like indexOf(), but it looks for an element starting from the end of the array.


JSON.parse()

JSON is often used to get data from a web server.

Picture getting this bunch of words from a web server:

'{"name":"John", "age":30, "city":"New York"}'

The JavaScript code JSON.parse() changes text into a JavaScript object.


JSON.stringify()

JSON is often used to transmit information to a web server.

When you send information to a web server, it needs to be in the form of a string.

Picture having this thing in JavaScript:

var obj = {name:"John", age:30, city:"New York"};

Employ the JavaScript function JSON.stringify() to transform it into a string.

var myJSON = JSON.stringify(obj);

The outcome will be a text formatted using JSON style.

myJSON is now in string format and prepared to be sent to a server:


Date.now()

The code Date.now() gives you the count of milliseconds passed since the zero date, which is January 1, 1970, 00:00:00 UTC.

The code Date.now() gives the same result as calling getTime() on a Date object.


Date toISOString()

The toISOString() function changes a Date object into a string, following the ISO standard format.


Date toJSON()

The toJSON() function changes a Date object into a string, arranged in the JSON date format.

JSON dates follow the ISO-8601 standard format: YYYY-MM-DDTHH:mm:ss.sssZ.


Property Getters and Setters

ES5 allows you to create object methods using a syntax that resembles defining or updating a property.

This example makes a getter for a property named fullName:

This illustration demonstrates how to establish a setter and a getter for the language property.

This illustration employs a setter to ensure that language updates are limited to uppercase letters.


Object.defineProperty()

The Object.defineProperty() is a recently introduced method for Objects in ES5.

You can set up a characteristic of an object, modify the value of a characteristic, and/or add additional information about a characteristic.

The following example shows the same code, but it conceals the language property when listing.

This example makes a method to safely change and retrieve uppercase language updates.


E5 Object Methods

ES5 introduced many new methods for objects in JavaScript.

Managing Objects

// Create object with an existing object as prototype
Object.create(parent, donor)

// Adding or changing an object property
Object.defineProperty(object, property, descriptor)

// Adding or changing object properties
Object.defineProperties(object, descriptors)

// Accessing Properties
Object.getOwnPropertyDescriptor(object, property)

// Returns all properties as an array
Object.getOwnPropertyNames(object)

// Accessing the prototype
Object.getPrototypeOf(object)

// Returns enumerable properties as an array
Object.keys(object)

Protecting Objects

// Prevents adding properties to an object
Object.preventExtensions(object)

// Returns true if properties can be added to an object
Object.isExtensible(object)

// Prevents changes of object properties (not values)
Object.seal(object)

// Returns true if object is sealed
Object.isSealed(object)

// Prevents any changes to an object
Object.freeze(object)

// Returns true if object is frozen
Object.isFrozen(object)

Function Bind()

The bind() method allows an object to use a method from another object.

This example makes two objects called "person" and "member."

The member object uses the fullname method from the person object.


Trailing Commas

ES5 permits the use of commas at the end of object and array definitions.

Object Example

person = {
  firstName: "John",
  lastName: " Doe",
  age: 46,
}

Array Example

points = [
  1,
  5,
  10,
  25,
  40,
  100,
];

WARNING !!!

JSON doesn't permit using commas at the end.

JSON Objects:

// Allowed:
var person = '{"firstName":"John", "lastName":"Doe", "age":46}'
JSON.parse(person)

// Not allowed:
var person = '{"firstName":"John", "lastName":"Doe", "age":46,}'
JSON.parse(person)

JSON Arrays:

// Allowed:
points = [40, 100, 1, 5, 25, 10]

// Not allowed:
points = [40, 100, 1, 5, 25, 10,]