Javascript array creation and common operations (1)

Source: Internet
Author: User

Are you familiar with operations on Javascript arrays? Here, I would like to share with you a brief introduction, including creating, adding, deleting, reading, and common methods and attributes of JavaScript arrays, I believe this article will surely help you gain some benefits.

Javascript array and operations

This article introduces the following aspects:

1. How to Create a JavaScript Array

2. How to add, delete, and read JavaScript arrays)

3. Common JavaScript Array methods and attributes

◆ How to create an array is generally divided into three types based on Initialization settings:

1. Create a JavaScript array:

Vararr = newArray ();

Key Point: use the new keyword to create an Array object Array (). The Array () object is a local class. You can use new to create an object and then use

2. Specify the array size when creating a JavaScript array:

Vararr = newArray (10); // an array with an initial size of 10 is created.

Note: When the array size is used for initialization, the array will be automatically extended without errors like in C. dynamic Growth is a property of js arrays. in addition, js supports a maximum length of 4294967295

3. Direct initialization:

Vararr = newArray ("crawler", "love", "Mao"); // The array or vararr = ["crawler", "love ", "Mao"]; // brackets can also declare an array object. Of course, similar to the C language, you can also define a 2-dimensional, 3-dimensional, and multi-dimensional array, which is not discussed here.

JavaScript array attribute: length

Arr. length returns the length of the array arr, which is common in traversing arrays in a loop. For example:

 
 
  1. For (vari = 0; I <arr. length; I ++ ){
  2. Execution part
  3. }

Access to array elements: arr [index], where index indicates the base number of the array. It starts from 0 and has a total of arr. length element. for example, if arr [0] accesses the first array element, arr [1] accesses the second array element .... operation Methods of arrays by analogy: first, we will give an overview of the common methods of these operation arrays (13)

 
 
  1. toString(),valueOf(),toLocalString(),join(),  
  2. split(),slice(),concat(),  
  3. pop(),push(),shift(),unshift(),sort(),splice() 

 


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.