САЙТ ДЛЯ ВЕБ РАЗРАБОТЧИКОВ НА РУССКОМ ЯЗЫКЕ

JavaScript ECMAScript 5



What is ECMAScript 5?

ECMAScript 5 is also known as ES5 and ECMAScript 2009

It is actually JavaScript version 1.8.5, but it is also called JavaScript 5.

This chapter introduces some of the most important features of ES5.

  • The "use strict" Directive
  • String.trim()
  • Array.isArray()
  • Array.forEach()
  • Array.map()
  • Array.filter()
  • Array.reduce()
  • Array.every()
  • Array.indexOf()
  • Array.lastIndexOf()
  • JSON.parse()

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.


String.trim()

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

Example

var str = "       Hello World!        ";
alert(str.trim());
Попробуйте сами »

Read more in JS String Methods.



Array.isArray()

Checks whether an object is an array.

Example

function myFunction() {
    var fruits = ["Банан", "Апельсин", "Яблоко", "Манго"];
    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 = [4, 9, 16, 25];
numbers.forEach(myFunction);

function myFunction(item, index) {
    txt = txt + item + "<br>";
}
Попробуйте сами »

Learn more in JS Array Iteration Methods.


Array.map()

The map() method creates a new array by performing a function on each array element.

This example multiplies each array value by 2:

Example

var numbers1 = [4, 9, 16, 25];
var numbers2 = numbers1.map(myFunction);

function myFunction(value, index, array) {
    return val * 2;
}
Попробуйте сами »


Array.filter()

The filter() method creates a new array with all array elements that passes a test.

This example creates a new array from elements with a value equal to or larger than 18:

Example

var numbers = [4, 9, 16, 25];
var over18 = numbers.filter(myFunction);

function myFunction(value, index, array) {
    return value > 18;
}
Попробуйте сами »

Array.reduce()

The reduce() method reduces an array to a single variable.

This example finds the sum of all numbers in an array:

Example

var numbers1 = [4, 9, 16, 25];
var sum = numbers1.reduce(myFunction);

function myFunction(total, value, index, array) {
    return total + value;
}
Попробуйте сами »

Array.every()

The every() method checks if all array values pass a test.

This example finds the sum of all numbers in an array:

Example

var numbers = [4, 9, 16, 25, 29];
var allOver18 = numbers.every(myFunction);

function myFunction(value, index, array) {
    return value > 18;
}
Попробуйте сами »

Array.indexOf()

Search an array for an element value and returns its position.

Example

var fruits = ["Банан", "Апельсин", "Яблоко", "Манго"];
var a = fruits.indexOf("Яблоко");
Попробуйте сами »

Learn more in JS Array Iteration Methods.


Array.lastIndexOf()

Array.lastIndexOf() is the same as Array.indexOf(), but searches from the end of the array.

Example

var fruits = ["Банан", "Апельсин", "Яблоко", "Манго"];
var a = fruits.lastIndexOf("Яблоко");
Попробуйте сами »

Learn more in JS Array Iteration Methods.


JSON.parse()

A common use of JSON is to exchange data with a web server.

Imagine you received this text string from a web server:

'{ "name":"Андрей", "age":30, "city":"New York"}'

The JavaScript function JSON.parse() is used to convert the text into a JavaScript object:

var obj = JSON.parse('{ "name":"Андрей", "age":30, "city":"New York"}');
Попробуйте сами »

The numbers in the table specify the first browser version that fully supports the method.

Method
parse() 4.0 8.0 3.5 4.0 11.5

Read more in JS JSON.