Array.prototype.includes()
method.Array.prototype.includes()
method is designed to check for the presence of an element in an array. Finding the required in the array, it returns true
, not finding - false
. Prior to ES7, the indexOf()
method served to perform the same operation, which returns, if an element is found, the first index by which it can be found in the array. If indexOf()
does not find the element, it returns the number -1
.-1
converted to true
. As a result, to check the results of the work of indexOf()
it was necessary to use a not very convenient construction of the following form. if ([1,2].indexOf(3) === -1) { console.log('Not found') }
indexOf()
does not find an element, returns false
, use something like the one shown below, the code will not work correctly. if (![1,2].indexOf(3)) { // console.log('Not found') }
![1,2].indexOf(3)
gives false
.includes()
method, such comparisons look much more logical. if (![1,2].includes(3)) { console.log('Not found') }
[1,2].includes(3)
returns false
, this value is an operator !
turns into true
and a message is received in the console stating that the required element in the array was not found.Math.pow()
method, but it is more convenient to use it than the library function, since it is part of the language. Math.pow(4, 2) == 4 ** 2 //true
Object.values()
method.Object.entries()
method.Object.getOwnPropertyDescriptors()
method.String
object — padStart()
and padEnd()
.padStart()
method fills the current line with another line until the final line reaches the desired length. Filling occurs at the beginning of the line (left). Here is how to use this method. str.padStart(targetLength [, padString])
str
is the current string, targetLength
is the length of the resulting string (if it is less than the length of the current string — this string will be returned unchanged), padString
— optional — the string used to populate the current string. If the padString
parameter padString
not specified, a space character is used to padString
current line to the specified length.padEnd()
method is similar to padStart()
, but the line is filled to the right. const str = 'test'.padStart(10) const str1 = 'test'.padEnd(10,'*') console.log(`'${str}'`) //' test' console.log(`'${str1}'`) //'test******'
padStart()
indicating only the desired length of the result line, spaces were added to the beginning of the source line. When using padEnd()
*
characters were added to the end of the source line with the indication of the length of the final line and the line. const person = { name: 'Fred', age: 87 } const personValues = Object.values(person) console.log(personValues) // ['Fred', 87]
[key, value]
, the keys and values ​​of the object's own properties. const person = { name: 'Fred', age: 87 } const personValues = Object.entries(person) console.log(personValues) // [['name', 'Fred'], ['age', 87]]
value
is the property value of the object.writable
- true
if the property can be changed.get
- contains the getter function associated with the property, or if there is no such function, undefined
.set
- contains the setter function for the property or undefined
.configurable
- if there is false
- the property cannot be deleted, its attributes cannot be changed except for the value.enumerable
— if this property contains true — the
is enumerable. Object.getOwnPropertyDescriptors(obj)
const person = { name: 'Fred', age: 87 } const propDescr = Object.getOwnPropertyDescriptors(person) console.log(propDescr) /* { name: { value: 'Fred', writable: true, enumerable: true, configurable: true }, age: { value: 87, writable: true, enumerable: true, configurable: true } } */
Object.assign()
method for copying objects, which appeared in the ES6 standard.console.log()
, what it is trying to write to its corresponding property. const person1 = { set name(newName) { console.log(newName) } } person1.name = 'x' // x
assign()
method. const person2 = {} Object.assign(person2, person1) person2.name = 'x' // ,
name
property, which in the original object was a setter, is now represented as a regular property.Object.defineProperties()
methods (it appeared in ES5.1) and Object.getOwnPropertyDescriptors()
. const person3 = {} Object.defineProperties(person3, Object.getOwnPropertyDescriptors(person1)) person3.name = 'x' //x
Object.assign()
are also characteristic of the Object.create()
method when it is used to clone objects. const doSomething = ( var1, var2, ) => { //... } doSomething( 'test1', 'test2', )
async/await
construction, which can be considered the most important innovation of this version of the language.async/await
construction solves the problem of promises and improves usability with asynchronous code. function doSomethingAsync() { return new Promise((resolve) => { setTimeout(() => resolve('I did something'), 3000) }) } async function doSomething() { console.log(await doSomethingAsync()) } console.log('Before') doSomething() console.log('After')
Before After I did something
doSomething()
program continues to run, after Before
, After is immediately output to the console, and after three seconds has passed, I did something
is displayed. function promiseToDoSomething() { return new Promise((resolve)=>{ setTimeout(() => resolve('I did something'), 10000) }) } async function watchOverSomeoneDoingSomething() { const something = await promiseToDoSomething() return something + ' and I watched' } async function watchOverSomeoneWatchingSomeoneDoingSomething() { const something = await watchOverSomeoneDoingSomething() return something + ' and I watched as well' } watchOverSomeoneWatchingSomeoneDoingSomething().then((res) => { console.log(res) // I did something and I watched and I watched as well })
Promise.prototype.finally()
.first
and second
constants, and all the others in the others
constant. const numbers = [1, 2, 3, 4, 5] const [first, second, ...others] = numbers console.log(first) //1 console.log(second) //2 console.log(others) //[ 3, 4, 5 ]
spread
operator allows you to pass arrays to functions that are waiting for normal parameter lists. const numbers = [1, 2, 3, 4, 5] const sum = (a, b, c, d, e) => a + b + c + d + e const res = sum(...numbers) console.log(res) //15
const { first, second, ...others } = { first: 1, second: 2, third: 3, fourth: 4, fifth: 5 } console.log(first) //1 console.log(second) //2 console.log(others) //{ third: 3, fourth: 4, fifth: 5 }
const items = { first, second, ...others } console.log(items) //{ first: 1, second: 2, third: 3, fourth: 4, fifth: 5 }
for-await-of
design allows you to call asynchronous functions that return promises in cycles. Such cycles are waiting for promise resolution before proceeding to the next step. Here's what it looks like. for await (const line of readLines(filePath)) { console.log(line) }
async/await
construction.then()
method is called. If something goes wrong, the catch()
method is called. The finally()
method allows you to execute some code, regardless of what happened before. fetch('file.json') .then(data => data.json()) .catch(error => console.error(error)) .finally(() => console.log('finished'))
?<=
). This allows you to search in the lines for some constructions, in front of which there are some other constructions.?=
Construct was in regular expressions implemented in JavaScript and up to the ES2018 standard. Such checks let you know if another fragment follows a fragment of a string. const r = /Roger(?= Waters)/ const res1 = r.test('Roger is my dog') const res2 = r.test('Roger is my dog and Roger Waters is a famous musician') console.log(res1) //false console.log(res2) //true
?!
performs the inverse operation — a match will be found only if there is no other line following the specified string. const r = /Roger(?! Waters)/g const res1 = r.test('Roger is my dog') const res2 = r.test('Roger is my dog and Roger Waters is a famous musician') console.log(res1) //true console.log(res2) //false
?<=
Construct is used. const r = /(?<=Roger) Waters/ const res1 = r.test('Pink Waters is my dog') const res2 = r.test('Roger is my dog and Roger Waters is a famous musician') console.log(res1) //false console.log(res2) //true
?<!
. const r = /(?<!Roger) Waters/ const res1 = r.test('Pink Waters is my dog') const res2 = r.test('Roger is my dog and Roger Waters is a famous musician') console.log(res1) //true console.log(res2) //false
\d
, the corresponding any digit, the class \s
, the corresponding any space character, the class \w
, which matches any alphanumeric character, and so on. The feature in question extends the set of classes that can be used in regular expressions, allowing you to work with Unicode sequences. We are talking about the class \p{}
and the opposite class \P{}
.\p{}
. So, for example, the Script
property determines the family of languages ​​to which a character belongs, the ASCII
property, logical, is true
for ASCII characters, and so on. For example, find out if certain strings contain exclusively ASCII characters. console.log(r.test('abc')) //true console.log(r.test('ABC@')) //true console.log(r.test('ABC')) //false
ASCII_Hex_Digit
property is true
only for characters that can be used to write hexadecimal numbers. const r = /^\p{ASCII_Hex_Digit}+$/u console.log(r.test('0123456789ABCDEF')) //true console.log(r.test('H')) //false
Uppercase
, Lowercase
, White_Space
, Alphabetic
, Emoji
.Script
property to determine which alphabet is used in a string. Here we check the string for use of the Greek alphabet. const r = /^\p{Script=Greek}+$/u console.log(r.test('ελληνικά')) //true console.log(r.test('hey')) //false
const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/ const result = re.exec('2015-01-02') console.log(result) /* [ '2015-01-02', '2015', '01', '02', index: 0, input: '2015-01-02', groups: { year: '2015', month: '01', day: '02' } ] */
const re = /(\d{4})-(\d{2})-(\d{2})/ const result = re.exec('2015-01-02') console.log(result) /* [ '2015-01-02', '2015', '01', '02', index: 0, input: '2015-01-02', groups: undefined ] */
s
flag causes the symbol to be .
(dot) will, among other things, match the newline character. Without the use of this flag, a dot matches any character except a newline character. console.log(/hi.welcome/.test('hi\nwelcome')) // false console.log(/hi.welcome/s.test('hi\nwelcome')) // true
Source: https://habr.com/ru/post/431872/
All Articles