JS Basic Learning Notes (iv)

Source: Internet
Author: User
Tags javascript array

4.1

What is an array

We know that variables are used to store data, that a variable can store only one content, and that an array is needed when storing multiple values.

An array is a collection of values, each with an index number, starting at 0 , with a corresponding value for each index, and adding more values as needed.

Syntax for creating an array:

var myarray = new Array ();

As we create arrays, we can also specify the length of the array, which can be arbitrarily specified.

var myarray= new Array (8); create an array that stores 8 data.

Attention:

1. the new array created is an empty array with no value, such as output, to display undefined.

2. Although a length is specified when an array is created, the array is actually long, meaning that the element can still be stored outside the specified length even if the length is specified as 8.

We can also create the above array and assignment in a simple way:

The first method:

var myarray = new Array (66,80,90,77,59);// Create array to assign value at the same time

The second method:

var myarray = [66,80,90,77,59];// input an array directly (called "literal array")

Note: The data stored in the array can be of any type (numbers, characters, booleans, etc.)

Add a new element to an array

You can add new elements to an array at any time by simply using the next unused index.

To get the value of an array element, simply reference the array variable and provide an index, such as:

The first person's performance expression method:Myarray[0]

The third person's performance expression method : Myarray[2]

Array Property length

If we want to know the size of the array, simply reference an attribute of the array length. The Length property represents the size of the array, which is the number of elements in the array.

Grammar:

Myarray.length; Get the length of the array myarray

Note: Since the index of an array always starts with 0, the upper and lower bounds of an array are: 0 and length-1, respectively. The length of an array is 5, and the upper and lower bounds of the arrays are 0 and 4, respectively.

At the same time, the length property of the JavaScript array is variable, which requires special attention.

arr.length=10; Increase the length of the array document.write (ARR.LENGTH); Array length has changed to 10

The length of the array changes as the element grows, as follows:

var arr=[98,76,54,56,76]; An array containing 5 values document.write (arr.length);  Displays the length of the array 5arr[15]=34; add element, use Index to 15, assign value to 34alert (arr.length); Displays the length of the array 16

Two-dimensional arrays

First-level arrays, we look at a set of boxes, each with only one content.

Representation of one-dimensional arrays: myarray[]

Two-dimensional arrays, we look at a set of boxes, but each box can also put more than one box.

Representation of a two-dimensional array: myarray[[]

Note: The index values for the two dimensions of a two-dimensional array are also starting from 0, and the last index value for two dimensions is length-1.

1. How to define a two-D array

var myarr=new Array ();  First declare one-dimensional for (Var i=0;i<2;i++) {//one-dimensional length of 2 myarr[i]=new Array ();   In the declaration two-dimensional for (Var j=0;j<3;j++) {//two-D length is 3 myarr[i][j]=i+j; Assignment, the value of each array element is i+j}}


Note: For the FOR Loop statement, see Chapter Fourth 4-5.

The above two-dimensional array is represented in a tabular way:

2. Method of defining two-dimensional arrays two

var myarr = [[0, 1, 2],[1, 2, 3]]

3. Assigning values

myarr[0][1]=5; The value of 5 is passed into the array, overwriting the original value.

Description: Myarr[0][1], 0 represents the table row, and 1 represents the table column.

------------------------------------------------------------------------------------------------------------

Precedence between operators (high-to-low):

Arithmetic operator → comparison operator → logical operator → "=" assignment symbol

If the operation of the sibling is done in the left-to-right order, the multi-level brackets are outward.

Arithmetic operators:

The precedence of a bitwise operation is the highest

Next is (+ + put in before and after the meaning is not the same, ++i means I first add 1 and then the next round to continue execution +1;i++ when the next round to start execution +1)

++

--

Then the

* /

The Last is

+ -

JS Basic Learning Notes (iv)

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.