Home > Web Front-end > JS Tutorial > body text

How to create a string array in js

coldplay.xixi
Release: 2023-01-03 09:26:31
Original
12523 people have browsed it

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");
Copy after login

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(",");// 在每个逗号(,)处进行分解。
Copy after login

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";
Copy after login

2: Concise method:

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

3: Literal:

var myCars=["Saab","Volvo","BMW"];
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!