On the _javascript techniques of JavaScript string and array

Source: Internet
Author: User
Tags array length javascript array

JavaScript string

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

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

Double quotes and single quotes must appear in pairs, which can contain single quotes, and can contain double quotes inside the single quotation mark.

For example:

Copy Code code as follows:

var mystr1= "My name is ' Xiaohua '!" ";
var mystr2= ' "Is my dream!", Tom said. ' ;

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

Copy Code code as follows:

Mystr1.length;
Mystr2.length;

JavaScript array

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

In JavaScript, you can define an array in several ways.

Use keyword new to create an array object

For example, create an array named MyArray and assign the value:

Copy Code code as follows:

var myarray=new Array ();
Myarray[0] = "zhangming";
MYARRAY[1] = "Zhaowei";
MYARRAY[2] = "Wanghua";

You can also assign values while creating an object:

Copy Code code as follows:

var myarray=new Array ("zhangming", "Zhaowei", "Wanghua");

Create an array directly using []

For example, create an array named MyArray and assign the value:

Copy Code code as follows:

var myarray=[];
Myarray[0] = "zhangming";
MYARRAY[1] = "Zhaowei";
MYARRAY[2] = "Wanghua";

Of course, you can also assign values at the same time you create an array:

Copy Code code as follows:

var myarray=["zhangming", "Zhaowei", "Wanghua"];

Create a group of key/value pairs

For example, create an array named MyArray and assign the value:

Copy Code code as follows:

var myarray=new Array (); You can also use Var myarray=[];
myarray["zhangming"] = "22";
myarray["Zhaowei"] = "21";
myarray["Wanghua"] = "30";

Modifying an array

Arrays can be modified after they are created and assigned, for example:

Copy Code code as follows:

Myarray[0] = "zhangming_1";
MYARRAY[1] = "zhaowei_1";
myarray["zhangming"] = "42";
myarray["Zhaowei"] = "61";

Array length

In JavaScript, you get the length of an array by length, for example:

Copy Code code as follows:

Myarray.length

The above mentioned is the entire content of this article, I hope you can enjoy.

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.