JS array creation and CONCAT () split () slice () The use of the method _javascript skills

Source: Internet
Author: User
Tags arrays

The use of JS array creation and CONCAT () split () Slice ()

1 var a = new Array ();

2 var b=new Array;

3 var c= new Array ("Red", "green", "white");

Alert (b.length)  //20

Arrays can grow or decrease as needed. So

C[3] = "purple";

Alert (c.length)//4

If

C[25]= "Purple"/is filled with null 

alert (c.length) from 3 to 24//26

You can also define an array object in literal notation, that is, by using square brackets and separating values with commas.

var d =["Red", "green", "white"];

Alert (d.length)  //3

d[25]= "PURPLR"

alert (d.length);//26

Note In this example, the array class is not explicitly used. The square brackets imply that the values are stored in the array object, and that the array declared in this way is the same as the array declared in the traditional way

The array object overrides the ToString () method and the ValueOf () method. Returns a special string.

var e =["Red", "green", "white"];

Alert (e.tostring ());  "Red,green,white"

alert (e.valueof ());  Ditto

Join ()

Alert (E.join ("-spring-"))  //"Red-spring-green-spring-white"

Split () method, string converts itself to an array

var s= "A,b,c";

var ss=s.split (",");//returns 3 arrays

Character-by parsing strings

var s= "Green"

var ss=s.split ("")

alert (ss.tostring ())  //Back to "G,r,e,e,n"

The array object has a method of two string classes, that is, the concat () and the Slice () method; The Concat method handles the array as if it were a string, and the parameter is appended to the end of the array, and the returned function value is the new Array object

The slice () method is the same as the string slice () method, returns a new array with a specific item: if there is only one argument, the modification returns all items starting at that position to the end of the array, and if there are two parameters, it returns all the values between the first position and the second bit. Items that do not include a second position

var s=["A", "B", "C"];

var scon=s.concat ("D", "E");

Alert (scon.tostring ())  //"A,b,c,d,e"

alert (s.tostring ())  //"A,b,c"

var s1=s.slice (1)  //s1 to " B,c " 

var s2=s.slice (0,2)//s2 to" A,b "

The above JS array creation and CONCAT () split () slice () is the use of small parts to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.