JavaScript implements the method of array and string mutual conversion _javascript techniques

Source: Internet
Author: User
Tags array definition

The examples in this article describe the way JavaScript implements array and string mutual conversion. Share to everyone for your reference, specific as follows:

The array class can be defined as follows:

Copy Code code as follows:
var avalues = new Array ();

If you know the length of an array in advance, you can pass the length with parameters
Copy Code code as follows:
var avalues = new Array (20);

The following 2 ways of defining are the same

Mode 1:

var acolors = new Array ();
Acolors[0] = "Red";
ACOLORS[1] = "green";
ACOLORS[2] = "Blue";
Alert (acolors[0]); Output "Red"

Mode 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.

Copy Code code as follows:
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.

I hope this article will help you with JavaScript programming.

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.