Article From:

//String loop for of
for(let s of  'helloworld'){    
  console.log(s)  //h e l l ....

2、includes(), startsWith(), endsWith()

  • includes():Returns a Boolean value indicating whether a parameter string has been found.
  • startsWith():Returns a Boolean value indicating whether the parameter string is at the head of the original string.
  • endsWith():Returns a Boolean value indicating whether the parameter string is at the end of the original string
let s = 'Hello world!';

s.startsWith('Hello') // true
s.endsWith('!') // true
s.includes('o') // true

All three methods support the second parameter, which indicates where the search begins.

let s = 'Hello world!';

s.startsWith('world', 6) // true
s.endsWith('Hello', 5) // true
s.includes('Hello', 6) // false

The code above indicates that the second parameter is usednAt that time,endsWithBehavior differs from the other two approaches. It aims at the front.nTwo characters, while the other two methods are for the followingnOne position until the end of the string.


repeatMethod returns a new string to repeat the original stringnSecond time.

'x'.repeat(3) // "xxx"
'na'.repeat(0) // ""
'na'.repeat(2.9) // "nana" Decimals are rounded down//If the parameter of repeat is negative or Infinity, an error will be reported.
// RangeError
// RangeError

However, if the parameter is a decimal between 0 and – 1, it is equal to 0, because the integer operation is performed first. The decimal number between 0 and – 1, when taken as an integral, equals-0repeatSee as 0.

'na'.repeat(-0.9) // ""
'na'.repeat(NaN) // ""        NaNEqual to 0
'na'.repeat(true) // "na"     tureEqual to 1
'na'.repeat(false) // ""      tureEqual to 0
'na'.repeat('2') //"nana"     Strings are converted to numbers first


ES2017 The function of string completion length is introduced. If a string is not long enough, it will be filled in at the head or tail.padStart()For head repair,padEnd()For tail repair.

//The original string length is less than the specified length
'x'.padStart(5, 'ab') // 'ababx'
'x'.padStart(4, 'ab') // 'abax'

'x'.padEnd(5, 'ab') // 'xabab'
'x'.padEnd(4, 'ab') // 'xaba'

//The original string length is longer than the specified length
'xxx'.padStart(2, 'ab') // 'xxx'
'xxx'.padEnd(2, 'ab') // 'xxx'

If the length of the complement string exceeds the specified minimum length of the original string, the complement string that exceeds the number of digits is truncated.

'abc'.padStart(10, '0123456789')
// '0123456abc'

If the second parameter is omitted, the blank completion length is used by default.

'x'.padStart(4) // '   x'
'x'.padEnd(4) // 'x   '

5, Template string

The template string is an enhanced version of the string, identified by a back quotation mark (`).

Embedding variables in Template Strings requires that variable names be written in${}In

$('#result').append(`There are <b>${basket.count}</b> items
   in your basket, <em>${basket.onSale}</em>are on sale!`);


Link of this Article: String extension