Javascript: variables and Arrays

Source: Internet
Author: User

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>



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.