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.