JavaScript notes: Basic knowledge

Source: Internet
Author: User

First, the data type

string example.

var str = ' Hello world '; str[5] = '. ' ; var len = str.length; var upperstr = str.touppercase (); var lowerstr = str.tolowercase (); var i = Str.indexof (' World '); var substr = str.substring (6, 11);
View Code

An array example.

vararr = [0, 1.0, ' 1 ',true, Undefined,NULL, []];varLen =arr.length;varelement = Arr.indexof (0);varSubarr = Arr.slice (0, 5);//add an element to the end or delete an elementArr.push (1, 2, 3); Arr.pop ();//add an element to the head or delete an elementArr.unshift (4, 5, 6); Arr.shift ();varDelarr = Arr.splice (0, 3, ' 7 ', ' 8 ', ' 9 ');//Insert after deleting contentArr.concat (Delarr);//Connection Arrayvarstr = arr.join ('-');//connect using the specified stringArr.sort (); Arr.reverse ();
View Code

An example object.

var Player = {    ,    ' Stephen Curry ',    ' Golden State Warriors ',}; player[// access Properties // Add properties Delete// Delete Properties inch // determines whether a property has // determines whether a property has its own
View Code

Second, Process Control

var arr = [];  for (var i = 0; i < i++)    Arr.push (i); var sum = 0;  for (var in arr)     if (j% 2 = = = 0)         + = parseint (j); alert (sum);
View Code

For...in loops due to historical legacy issues, which traverse the actual object's property name. When we manually add some extra attributes to the object, the for...in loop will also traverse the attributes we added as collection elements. The For...of loop solves these problems by traversing only the collection elements themselves.

var arr = [0, 1, 2, 3, 4= "MyArray"; var str = ';  for (var  I of arr)     + = I;alert (str);
View Code

Three, set and mapping

The default object representation of JS is visible as a insinuate data structure in other languages, but the problem is that the health value must be a string. The ES6 specification introduces a new collection and mapping data type.

var New map (); Map.set (1, "Spring"); Map.set (2, "Summer"); map. Delete (1); var New set (); Set.add ("Autumn"); Set.add ("Winter"); set. Delete ("Autumn");
View Code

JavaScript notes: Basic knowledge

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.