JavaScript variable array reference type

Source: Internet
Author: User
Tags array sort

A global variable and a local variable global variable: (1) inside a variable (2) method declared outside the method, there are no variables declared with the VAR keyword
Local variables: Inside the method, variables declared with Var
<script type= "Text/javascript" >
var a=3; Global variables
function Test () {
var a=2; Local variables
alert (a);
}
Test ();
</script>
Results:

If you comment out var a=2;

<script type= "Text/javascript" >
var a=3; Global variables
function Test () {
var a=2; Local variables
alert (a);
}
Test ();
</script>

Results:

If you change the program to:

<script type= "Text/javascript" >
function Test () {
c=5; Equivalent to a global variable
}
alert (c);
Test ();
</script>

The result is:

C is a global variable, but executes the function test () before it executes to C. In other words, JavaScript is performed from the top down. Two data types 1. Basic Data types: number,boolean,string,undefined,nullnumber: Integers, decimals, nan,infinity (positive infinity),-infinity (negative infinity) Undefined: Represents a variable declaration but does not assign a value of NULL: Represents an empty object reference example: var a=1/0; No error will be returned Infinity2. Reference type: Object class (such as objects, arrays, REGEXP, Date ...) 3.typeof operator: Used to determine the data type var a=10;alert (typeof a); The page shows the number where the base type is accessed by value and the reference type is accessed by reference.    A three-array JS array is similar to a Java container, with variable lengths and element types that can be different <script type= "Text/javascript" > var arr=[1,false];    var Result=arr.push (2,true, "abc");    Alert (arr); Alert (Result);</script>: where the push () function adds an element to the array and returns the length of the new array <script type= "Text/javascript" > var arr=[1,    false,2, "DFR"];    var obj=arr.pop ();    Alert (arr); Alert (obj);</script> results:

where the pop () function can remove an element from the end of the array and return the removed element value shift () function removes an element from the head, the Unshift () function inserts multiple elements from the head, and returns the length of the new array <script type= "text/ JavaScript >    var arr=[1,2,4,6,3];    Arr.splice (1, 2,3,4,5);    alert (arr); </ Script> results: Where the first parameter of splice () represents the starting position, the second parameter represents the number of intercepts, and the third parameter represents the new element to append if there are only two parameters, Arr.splice (for each); The result is that if the program is changed to: <script Type= "Text/javascript" >    var arr=[1,2,4,6,3];    Arr.slice (2,4);    alert (arr); </script> results: Slice () function intercept range: Left closed right open interval, do not manipulate the array itself, return the intercepted content, and the splice method will manipulate the array itself to modify the program: <script type= "Text/javascript" >    var arr=[1,2,4,6,3];    var result=arr.slice (2,4);    alert (result); </script > Results: <script type= "Text/javascript" >    var arr1=[1,2,3];    var arr2=[2,3,4];    var result=arr1.concat (ARR2);    alert (Result);</script> results: Similarly, concat () merge paste, do not manipulate the array itself < Script type= "Text/javascript" >    var arr1=[1,2,3];    var result=arr1.join ('-'); &NBsp   Alert (ARR1)     alert (Result);</script> results are: the join () method adds content between each element, and does not manipulate the array sort () method to sort in a positive order, reverse () Method Reverse sort Four reference type <script type= "Text/javascript" >    var obj={};    Obj.name= ' Zhang San ';    obj.age=20;    obj.say=function () {        alert (' Hello World ');   }    Alert (obj.name);    Obj.say (); for (var attr in obj) {        alert (attr);    }</script> Note: The For loop iterates through the properties of obj, that is, the value of attr is Name,age,say if you want to traverse the value, change to obj (attr)

JavaScript variable array reference type

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.