On the conversion of JavaScript array and string (recommended) _javascript tips

Source: Internet
Author: User
Tags array definition javascript array

The array class can be defined as follows:

var avalues = new Array ();

If you know the length of an array in advance, you can pass the length with parameters

var avalues = new Array (20);

------------------The following 2 definitions are the same--------1-------

var acolors = new Array ();

Acolors[0] = "Red";

ACOLORS[1] = "green";

ACOLORS[2] = "Blue";

Alert (acolors[0]); Output "Red"

-------------------------------------------------2-------

var acolors = new Array ("Red", "green", "blue"); and array definition arrays are equivalent.

Alert (acolors[0]); Output "Red" too

--------------------------

(1) Array converted to string

The above 2 arrays are defined in such a way that the output is the same, and a comma separator is found in the middle.

Alert (acolors.tostring ()); Output "Red,green,blue";

(2) string converted to array

We found that the array was converted to a string, with 1 more delimiters between the arrays ', ', and that string was converted to array arrays and must have delimiters. Can be a comma, or it can be another separator.

var scolors = "Red,green,blue";

var acolors = Scolors.split (', '); The string is converted to an array.

The above analysis of JavaScript array and string conversion (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.