Home >Web Front-end >JS Tutorial >A brief discussion on JavaScript strings and arrays_javascript skills

A brief discussion on JavaScript strings and arrays_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 15:56:551244browse

JavaScript String

A string is a collection of characters, including English letters, punctuation marks, special symbols, Chinese characters, etc.

In JavaScript, strings can be represented using double quotes (" ") or single quotes (' ').

Double quotes and single quotes must appear in pairs. Double quotes can contain single quotes, and single quotes can also contain double quotes.

For example:

Copy code The code is as follows:

var myStr1=" My name is ' xiaohua ' ! ";
var myStr2=' " This is my dream ! " , Tom said. ' ;

The length of the string is obtained by length, for example:

Copy code The code is as follows:

myStr1.length;
myStr2.length;

JavaScript Array

Arrays are used to store a series of values ​​in separate variables.

In JavaScript, arrays can be defined in the following ways.

Use keyword new to create array objects

For example, create an array named myArray and assign a value:

Copy code The code is as follows:

var myArray=new Array();
myArray[0] = " zhangming ";
myArray[1] = " zhaowei ";
myArray[2] = " wanghua ";

You can also assign values ​​while creating the object:

Copy code The code is as follows:

var myArray=new Array(" zhangming " , " zhaowei " , " wanghua ");

Use [ ] to create an array directly

For example, create an array named myArray and assign a value:

Copy code The code is as follows:

var myArray=[];
myArray[0] = " zhangming ";
myArray[1] = " zhaowei ";
myArray[2] = " wanghua ";

Of course, you can also assign values ​​while creating the array:

Copy code The code is as follows:

var myArray=[ " zhangming " , " zhaowei " , " wanghua " ];

Create an array of key/value pairs

For example, create an array named myArray and assign a value:

Copy code The code is as follows:

var myArray=new Array(); // You can also use var myArray=[ ];
myArray["zhangming"] = " 22 ";
myArray["zhaowei"] = " 21 ";
myArray["wanghua"] = " 30 ";

Modify array

Arrays can be modified after creation and assignment, for example:

Copy code The code is as follows:

myArray[0] = " zhangming_1 ";
myArray[1] = " zhaowei_1 ";
myArray["zhangming"] = " 42 ";
myArray["zhaowei"] = " 61 ";

Array length

In JavaScript, use length to get the array length, for example:

Copy code The code is as follows:

myArray.length

The above is the entire content of this article, I hope you all like it.

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