Basic Javascript Tutorial: array _ basics-js tutorial

Source: Internet
Author: User
Array is a common type in JavaScript, and arrays in JavaScript differ greatly from arrays in other languages. The data types stored in arrays in JavaScript are not necessarily the same, and the length of arrays can also be changed. String, value, and boolean values all belong to discrete values (scalar). If a variable is discrete, it has only one value at any time.

If you want to store a group of values using variables, you need to use an array ).

An array is a collection composed of multiple tree values with the same name. Each array in the set is an array element. You can use the variable team to store the names of each member in the team.

In JavaScript, arrays are created using the keyword Array declaration. Colleagues can also declare the length of a variable. For example

The Code is as follows:


Var aTeam = new Array (12); // declare the length of the Variable

When you cannot predict the final number of arrays, you can declare that the number of arrays is not specified. For example:

The Code is as follows:


Var aTeam = new Array (); // if the final number of arrays is unknown, the specific number cannot be declared.
ATeam [0] = 1414;
ATeam [1] = "Beijing ";
ATeam [2] = 0x4;
ATeam [3] = "I can ";
ATeam [4] = "red ";
ATeam [5] = "blue ";
ATeam [6] = "orange ";

In addition, you can directly create an array

The Code is as follows:


Var aTeam = new Array ("111", "blue", "red", "beijing ");

Like a string, an array can also use length to obtain and specify the length of an array.

The Code is as follows:


Var aTeam = new Array ("111", "blue", "red", "beijing ");
Document. write (aTeam [1] +"
");
Document. write (aTeam. length +"
")

Note: You can better understand arrays.

The Code is as follows:


Var aTeam = new Array ("111", "blue", "red", "beijing ");
ATeam [20] = "12415"
Document. write (aTeam [20] +"
");
Document. write (aTeam. length +"
")

In addition, Arrays can be defined using. Separated by commas.

The Code is as follows:


STeam = [10, "5565", "Beijing", 33263, "red"]
Document. write (sTeam [3]) // output 33263

You can use toString () to convert arrays.

The Code is as follows:


STeam = [10, "5565", "pking", 33263, "red"]
Document. write (sTeam. toString ())//
// Output result 10, 5565, pking, 33263, red
Document. write (typeof (ss ));
// Output result string

If the array is converted to a string and you do not want to use a comma to join, you can use the join () method.

The Code is as follows:


STeam = [10, "5565", "pking", 33263, "red"]
Ss = sTeam. join ("-");
Dd = sTeam. join ("] [")
// Output result 10, 5565, pking, 33263, red
Document. write (ss );
Document. write (dd );
// Output 10-5565-pking-33263-red 10] [5565] [pking] [33263] [red

For strings,JavaScript converts split () to an array

The Code is as follows:


Var fruit = "apple, 2151, orange ";
Sfruit = fruit. split (",")
Document. write (sfruit); // output apple, 2151, orange
Document. write (sfruit. join ("-"); // output apple-2151-orange

Example,Javascript provides the reverse () method to reverse the array.

The Code is as follows:


Var fruit = "apple, 2151, orange ";
Sfruit = fruit. split (",")
Document. write (sfruit); // output apple, 2151, orange
Document. write (sfruit. join ("-") +"
"); // Output apple-2151-orange
Document. write (sfruit. reverse () +"
"); Output orange, 2151, apple
Document. write (sfruit. reverse (). toString () +"
"); Output apple, 2151, orange

There is no direct conversion method for string inversion in javascript. We can convert the string into an array using split (), reverse using rerverse (), and join to reverse the string.

The Code is as follows:


Var fruit = "2151, orange, apple ";
Var sfruit = "iambeijing123 ";
Document. write (fruit. split (","). reverse () +"
"); // 2151, orange, apple
Document. write (sfruit. split (""). reverse (). join ("") +"
"); // Output iambeijing123

You can use sort () to sort array elements in alphabetical order.

The Code is as follows:


Fruit = ["orange2", "2151", "orange", "apple"]
Document. write (fruit. sort (); // output result 2151, apple, orange, orange2

Usage of push () and pop ()

The Code is as follows:


Sfruit = new Array ();
Sfruit. push ("red ");
Sfruit. push ("green ");
Sfruit. push ("oragen ");
Sfruit. push ("blue ");
Document. write (sfruit. length + sfruit. toString () +"
");
Var wfruit = sfruit. pop ();
Document. write (wfruit +"
")
Document. write (sfruit. toString ())

As shown above, javascript regards the array as a stack and uses push () and pop () to process the pressure stack and output stack.

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.