An array variable declaration of JavaScript Learning notes

Source: Internet
Author: User
Tags arrays

In fact, the development of the people, touch JavaScript, there is a paradoxical feeling, many of the grammar of JavaScript are very familiar with the structure of the programming and other mainstream language is not very different. But the main point is that JavaScript does not need to declare the type of a variable when declaring a variable, and to see the type of the variable directly through the composition of the variable.

Such as:

The code is as follows Copy Code
var str= "This is some string";

Variable STR is a string that declares a variable with a keyword var.

For arrays, the variables are declared as follows:

The code is as follows Copy Code
var a=new array ();
a[0]=1.2;
A[1]= "JavaScript";
A[2]=true;
A[3]={x:1,y:2};
A[4]=new Array (1,2,3);


It declares an array A and assigns it, which contains 5 elements, as in languages like C#,java, where the starting position of the array starts at 0. But significantly different from other languages, the type of each element can be different in an array of JavaScript. Furthermore, it is known from above that the declaration of an array or a string is represented by the keyword var.
And, from the 5th element a[4]=new Array (1,2,3), you can see that arrays can be nested, that is, the elements of an array can be a new array, so that the multidimensional array is made up and does not affect the other array elements at all!

For arrays, there are other ways to declare them:

The code is as follows Copy Code
var b=[1.2, "javascipt,true,{x:1,y:2},[1,2,3]"];


The array b above, exactly the same as the number of elements contained in array A, is 5, and it is clear that the second statement is clearly more concise than the previous one! So, if you've learned other languages, it's very fast for JavaScript to get into the state.

Related Article

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.