Js basic data type, var variable, array

Source: Internet
Author: User

(a) Js data type

Divided into basic data types and reference types

The basic data types are null,undefined,boolen,number,string, and there is a complex data type Object.

        var var1 = "Xianrongbin", Var2 = false, Var3 = null, VAR4 = undefined, var5 = 123;        Console.log (typeof VAR4);        var var6 = [1, 5];        Console.log (typeof VAR6);

Here, in fact, just a Var, in order to distinguish between the basic data types and complex data types, wrote two var keywords.

Defining all variables behind a var is useful for improving the efficiency of the program .

If Var is not labeled in a function, the variable is a global variable and is a variable of window because all global variables are assumed by window.

function TestVar () {            num = 1;        }        TestVar ();        Console.log (Window.num);

Here output 1, if the "num" before the addition of Var, the output will be an error.

The basic data type and the value type, one is the value, the other is the change address.

  var var6 = [1, 5];            function ChangeValue (obj) {            obj.pop ();        };                ChangeValue (VAR6);        Console.log (VAR6);         var num = 1;         function changenum (num) {            = num + 1;        };        Console.log (num);
Code

The value of the output is [1,23] 1. This is important. There's a big difference here in C #.

(ii) Array manipulation

The array type is object.

To add one or more elements to the end of the array, push () is available,

Delete the last element, available pops (),

Deletes an element of the array header, which can be Shif (),

Add one or more elements to the array header, using Unshift,

You need to know the position of an element in the array, use indeof (element name), no return-1,

After you know the position of the element, you need to delete an element, using splice (index,1),

If you need to delete an element and replace it with 1 or more elements, use Splice (index,1,a,b ...),

If you need to insert 1 or more elements after an element, use Splice (index,0,a,b ...) and just turn the 1 above into 0.

The contact method should be noted here.

All of the above methods change the original array properties, that is, change the pointer; Contactdoes not change the original array, just returns a copy of the original array .

Look at the results of Testarray after the following operation

        var testarry = [1, 2, 3, 4, 5, 6];        Testarry.push (7);              Testarry.pop ();        Testarry.shift ();        Testarry.unshift (' unshift1 ', ' unshift2 ');        Concatarry= Testarry.concat (' concat1 ');         var index = testarry.indexof (' concat1 ');         1);        Testarry.splice (0, 1, ' Replace1 ', ' replace2 ');
array operation basic function

The results are: ["Replace1", "Replace2", "unshift2", 2, 3, 4, 5]

    

(iii) Five iterative functions

These five iteration functions are. Every. Some. filter. ForEach. Map they all have three parameters (Item,key,value), key is counted from 0, and the latter two are optional.

  . Every () runs the given function for each item in the array, and returns True if the function returns true for each item

. Some () runs the given function for each item in the array, and returns True if the function returns true for any of the entries

  . The filter () runs the given function for each item in the array, and returns a list of the items that the function returns True.

  . ForEach () runs the given function for each item in the array, and the method does not return a value

  . Map () runs the given function for each item in the array, and returns a list of the results of each function call.

Use the following

varTestarry = [1, 2, 3, 4, 5, 6]; varIsAllBig2 = Testarry.every (function(item) {returnItem > 2;        });        Console.log (ISALLBIG2); varIsSomeBig2 = Testarry.some (function(item) {returnItem > 2;        });        Console.log (ISSOMEBIG2); varFilterarray = Testarry.filter (function(item) {returnItem > 2;        });               Console.log (Filterarray); varMaparray = Testarry.map (function(item) {returnItem + 2;        });        Console.log (Maparray); varForeachresult = Testarry.foreach (function(item) {if(Item >5) {Console.log (item); }        });

Where,. Foreach is no return value, and several others have a return value, this needs to be noted.

The above output results were

False
True
[3, 4, 5, 6]
[3, 4, 5, 6, 7, 8] 6

Js basic data type, var variable, array

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.