The fill () method of JavaScript

Source: Internet
Author: User

Inadvertently see fill this method, some puzzled, at first thought is someone else's custom method, later discovered that originally is not, JavaScript inside is really has this method, therefore specially studied under.

The fill () method is used to replace elements in the array with a fixed value. The fixed value can be a letter, a number, a string, an array, and so on. The Fill () method also has two optional parameters that indicate the starting and ending positions of the fill.

For example, replace the elements in the array with numbers.

var arr3 = [12,23,34,45,56];arr3.fill (123); Console.log (ARR3)  //  [ 123,123,123,123,123]

  

Replace the elements in the array with strings.

var arr3 = [12,23,34,45,56];arr3.fill ("Hello"); Console.log (ARR3)  //  [" Hello "," Hello "," Hello "," Hello "," Hello "]

  

Use an array to replace the elements in the array.

var arr3 = [12,23,34,45,56];arr3.fill ([123]); Console.log (ARR3)  //  [ [123], [123], [123], [123], [123]]

Specifies the starting and ending positions of the substitution.

var arr3 = [12,23,34,45,56];arr3.fill (1234,1,3); Console.log (ARR3)  //  [ 1234, 1234 , A.

1 represents the starting position of the substitution, and 3 indicates the end position of the substitution, and it can be seen that the element containing index 3 is not included.

Full usage: Fill (value,start,end) where the 2nd, 3 parameters are optional. Indicates the start and end position of the substitution (note: The element containing the end position itself is not included)

Note: the fill () method is not supported for IE11 and earlier versions.

The fill () method of JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.