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.