Javascript ES5 tutorial
- Get link
- X
- Other Apps
Array every() |
Array filter() |
Array forEach() |
Array indexOf() |
Array lastIndexOf() |
Array map() |
Array reduce() |
Array reduceRight() |
Array some() |
Array.isArray() |
Date toISOString() |
Date toJSON() |
Date.now() |
Function bind() |
JSON.parse() |
JSON.stringify() |
Multiline strings |
Object defineProperty() |
Object methods |
Property getters and setters |
Reserved words as property names |
String.trim() |
String[number] access |
Trailing commas |
Use strict |
ES5 Features topics | Explanation | ES5 Features Code Example |
Array every() | Tests whether all elements in the array pass the test implemented by the provided function. | var allPositive = [1, 2, 3].every(function(number) { return number > 0; }); // true |
Array filter() | Creates a new array with all elements that pass the test implemented by the provided function. | var filtered = [1, 2, 3, 4, 5].filter(function(number) { return number > 2; }); // [3, 4, 5] |
Array forEach() | Executes a provided function once for each array element. | [1, 2, 3].forEach(function(number) { console.log(number); }); // logs 1, 2, 3 |
Array indexOf() | Returns the first index at which a given element can be found in the array, or -1 if it is not present. | var index = [1, 2, 3].indexOf(2); // 1 |
Array lastIndexOf() | Returns the last index at which a given element can be found in the array, or -1 if it is not present. | var lastIndex = [1, 2, 3, 2, 1].lastIndexOf(2); // 3 |
Array map() | Creates a new array with the results of calling a provided function on every element in this array. | var squared = [1, 2, 3].map(function(number) { return number * number; }); // [1, 4, 9] |
Array reduce() | Apply a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value. | var sum = [1, 2, 3].reduce(function(total, number) { return total + number; }, 0); // 6 |
Array reduceRight() | Apply a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value. | var flattened = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) { return a.concat(b); }, []); // [4, 5, 2, 3, 0, 1] |
Array some() | Tests whether at least one element in the array passes the test implemented by the provided function. | var hasNegative = [1, 2, 3, -1, 4].some(function(number) { return number < 0; }); // true |
Array.isArray() | Determines whether the passed value is an Array. | var isArray = Array.isArray([1, 2, 3]); // true |
ES5 Features topics | Explanation | ES5 Features Code Example |
Date toISOString() | Converts a date to a string following the ISO 8601 Extended Format. | var dateStr = new Date().toISOString(); // 'YYYY-MM-DDTHH:mm:ss.sssZ' |
Date toJSON() | Returns a string representation of the Date object in JSON format, which is actually the same as the Date's toISOString method. | var jsonDate = new Date().toJSON(); // 'YYYY-MM-DDTHH:mm:ss.sssZ' |
Date.now() | Returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC. | var timestamp = Date.now(); // e.g., 1367133733765 |
Function bind() | Creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. | var obj = { x: 42 }; function func() { return this.x; } var boundFunc = func.bind(obj); console.log(boundFunc()); // 42 |
JSON.parse() | Parses a JSON string, constructing the JavaScript value or object described by the string. | var obj = JSON.parse('{"name":"John"}'); // {name: "John"} |
JSON.stringify() | Converts a JavaScript object or value to a JSON string. | var jsonStr = JSON.stringify({name: "John"}); // '{"name":"John"}' |
Multiline strings | Allows strings to be written across multiple lines without using string concatenation. | var multiStr = 'This is a \\ multiline string'; |
Object defineProperty() | Adds the named property described by a given descriptor to an object. | Object.defineProperty(obj, 'key', { value: 'static', writable: false }); obj.key; // 'static' |
Object methods | Refers to the new methods added to the Object constructor: create, defineProperty, defineProperties, freeze, getOwnPropertyDescriptor, etc. | var descriptors = Object.getOwnPropertyDescriptor({name: 'John'}, 'name'); console.log(descriptors.value); // 'John' |
Property getters and setters | Allows to define object properties that are accessed through getter/setter methods but look like regular properties to an external code. | var obj = { get x() { return 42; }, set x(v) { console.log('Setting x to', v); } }; |
Reserved words as property names | In ES5, reserved words like delete, void, return, etc., can be used as property names without error. | var obj = { 'delete': function() { /* ... */ }, 'void': 42 }; |
String.trim() | Removes whitespace from both ends of a string. | var trimmed = ' hello '.trim(); // 'hello' |
String[number] access | Accesses the character in the string at the specified index using bracket notation. | var character = 'hello'[1]; // 'e' |
Trailing commas | Allows commas to be placed after the last parameter in an array or object literal without causing an error. | var arr = [1, 2, 3,]; var obj = { foo: 'bar', }; |
Use strict | Directs the browser to enforce strict parsing and error handling of your JavaScript code. | 'use strict'; var undeclaredVariable = 'This will throw an error if strict mode is on'; |
- Get link
- X
- Other Apps
Comments
Post a Comment