Javascript: variables and Arrays
Javascript variables are of a weak type, that is, when declaring a variable, you do not need to specify the type, and then you can copy any type of value to it.
Code organized from w3school: http://www.w3school.com.cn
:
Sample Code:
Javascript Variables (1) js statements are executed according to their location in html.This is a paragraph before script.
<Script> document. write ("Javascript! "); </Script>This is a paragraph after script.
(2) js is weak type <script> var x; document. write ("x =" + x); document. write ("
") X = 2; document. write (" x = "+ x); document. write ("
") X =" hello "document. write (" x = "+ x); document. write ("
") </Script> (3) js-defined Array <script> var arr = new Array (); arr [0] = 1; arr [1] = 2; arr [2] = 3; document. write ("arr [0] =" + arr [0] + "arr [1] =" + arr [1] + "arr [2] =" + arr [2] + "arr [4] =" + arr [4]); // other methods for creating Arrays: document. write ("
"); Var arr2 = new Array (" Tom "," Smith "," John "); var arr3 = [" Tom "," Smith "," John "]; document. write ("arr2 [0] =" + arr2 [0]); document. write ("
"); Document. write (" arr3 [0] = "+ arr3 [0]); </script>The data in JS does not have the "subscript out of bounds" problem. (Undefine Will be obtained if the array size is exceeded) (4) js object <script> // create the object var person = {"firstName": "Will", "lastName ": "Smith"} // you can use either of the following methods to obtain the object property value: var name1 = person. firstName var name2 = person ["firstName"] document. write ("name1:" + name1 + ", name2:" + name2) </script> (5) Undefined and Null <script> var var1; var var2 = null; var var3 = "hello" document. write ("var1 =" + var1 + ", var2 =" + var2 + ", var3 =" + var3); document. write ("
"); Var3 = null; document. write (" var3 = "+ var3 +"
"); </Script> (6) Declare the variable type <script> var x = new String; var y = new Number; var z = new Array; x = "hello" y = 6 z = ["Tom", "John", "Smith"] // output variable type document. write ("x:" + typeof x +"
"); Document. write (" y: "+ typeof y +"
"); Document. write (" z: "+ typeof z +"
"); X = 2; document. write (" set x = 2, x: "+ typeof x +"
"); </Script>