Little notes about JavaScript syntax

Source: Internet
Author: User

1. The pop() function is used to move the last element in the group; the shift() function moves the first item in the array, and unshift (moves in) an element to the head of the arrays.

For example:

varMyArray = [["John", at], ["Dog",3]];varA=myarray.pop ();//var myArray = [["John", 23]]; Move out the lastvarA=myarray.shift ();//var myArray = [["Dog", 3]]; move out of the firstvarC=myarray.unshift (["CCC",1]);//var myArray =[["CCC", 1],["John", [["Dog", 3]]; Array ["CCC", 1] inserted the head, became the first

2, all the comparison operators return a Boolean value, or it is either true false . Use if/else statements to do comparisons and then return true or false have become a consensus among everyone:

For example:

function IsEqual (b) {  if (a = = b)    {returntrue;   Else  {    returnfalse;}  }

can be simplified into:

function IsEqual (b) {  return a = = = b;}

3, there are two ways to access the object properties, one is the dot operator ( . ), one is the bracket operator ( [] ). When you know the name of the property, use the dot operator.

1) dot operator ( . )

For example:

varMYOBJ ={ Prop1:"Val1", prop2:"Val2"};varProp1val = MYOBJ.PROP1;//Val1varProp2val = MYOBJ.PROP2;//Val2//orvarTestobj = {   "Hat": "Ballcap", "shirt": "Jersey","Shoes":"Cleats"};varHatvalue = Testobj.hat;//BallcapvarShirtvalue = Testobj.shirt;//jersey

2) Bracket operator ( [] ): If the name of the property you want to access has a space, you can only use the bracket operator ( [] ).

varMYOBJ = {  "Space Name":"Kirk",  "More Space":"Spock"};myobj["Space Name"];//Kirkmyobj['More Space'];//Spock

Tip: If there are spaces in the attribute name, you must enclose the attribute name in single or double quotation marks.

Little notes about JavaScript syntax

Related Article

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.