JavaScript string and array _ javascript tips-js tutorial

Source: Internet
Author: User
This article briefly introduces the strings and arrays in javascript, which are basic knowledge. To learn javascript well, you must understand these two points. JavaScript string

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

In JavaScript, strings can be expressed using double quotation marks ("") or single quotation marks.

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

For example:

The Code is as follows:


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

The length of a string is obtained by length. For example:

The Code is as follows:


MyStr1.length;
MyStr2.length;

JavaScript Array

An array is used to store a series of values in individual variables.

In JavaScript, you can use the following methods to define arrays.

Use the keyword new to create an array object

For example, create an array named myArray and assign values:

The Code is as follows:


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

You can also assign values when creating an object:

The Code is as follows:


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

Use [] to directly create an array

For example, create an array named myArray and assign values:

The Code is as follows:


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

Of course, you can also assign values when creating Arrays:

The Code is as follows:


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

Create a key/value pair Array

For example, create an array named myArray and assign values:

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 an array

Arrays can be modified after they are created and assigned values. For example:

The Code is as follows:


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

Array Length

In JavaScript, length is used to obtain the array length, for example:

The Code is as follows:


MyArray. length

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

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.