Home  >  Article  >  Web Front-end  >  How to create a string array in js

How to create a string array in js

coldplay.xixi
coldplay.xixiOriginal
2021-03-01 17:11:4812659browse

JS method to create a string array: 1. Create an empty array and assign it to a string. The code is [var arr=[];var arr=new Array("1","sdf ")]; 2. The implementation method is to cut the string into several strings according to a certain character and return it in the form of an array.

How to create a string array in js

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, DELL G3 computer. This method is suitable for all brands of computers.

JS method of creating a string array:

1. Create an empty array and assign it to a string:

var arr=[];
var arr=new Array("1","sdf");

2. The implementation method is to cut the string into several strings according to a certain character and return it in the form of an array. The sample code is as follows:

var s = "abc,abcd,aaa";
ss = s.split(",");// 在每个逗号(,)处进行分解。

Create an array, There are three methods

The following code defines an array object named myCars:

1: Conventional method:

var myCars=new Array(); 
myCars[0]="Saab";       
myCars[1]="Volvo";
myCars[2]="BMW";

2: Concise method:

var myCars=new Array("Saab","Volvo","BMW");

3: Literal:

var myCars=["Saab","Volvo","BMW"];

You can have different objects in an array

All JavaScript variables are objects. Array elements are objects. Functions are objects.

So, you can have different variable types in the array.

You can contain object elements, functions, and arrays in an array.

Related free learning recommendations: javascript video tutorial

The above is the detailed content of How to create a string array in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn