1.js Basic Syntax

Source: Internet
Author: User

1.Js basic Syntax 1. Data type JavaScript does not distinguish between integers and floating-point numbers, and uniformly uses number to denote Nan; Nan represents not a number, and Nan is used to denote Infinity when the result cannot be computed; Infinity represents infinity, which is represented as infinity 2 when the value exceeds the maximum value that the JavaScript number can represent. Comparison operators pay special attention to equality operator = =. JavaScript is designed with two comparison operators: the first is = = comparison, which automatically converts data types and compares them, often with very bizarre results, and the second is the = = = comparison, which does not automatically convert data types, if the data type is inconsistent, returns false, if consistent, and then compare. Because of the design flaws of JavaScript, do not use = = comparison, always adhere to the use = = = comparison. Another exception is that the special number of Nan is not equal to all other values, including its own: Nan = = = Nan; False the only way to judge NaN is through the IsNaN () function: IsNaN (NaN); True finally pay attention to the equal comparison of floating-point numbers: 1/3 = = = (1-2/3); False this is not a design flaw for JavaScript. Floating-point numbers generate errors during operation because the computer cannot accurately represent infinite loop decimals. To compare two floating-point numbers for equality, you can only calculate the absolute value of their difference to see if they are less than a certain threshold: Math.Abs (1/3-(1-2/3)) < 0.0000001; Truenull and Undefinednull represent an "empty" value, which differs from 0 and an empty string ', 0 is a numeric value, ' ' represents a string of length 0, and null means ' null '. In other languages, there are also JavaScript-like null representations, such as Java, which is also used with the Null,swift Nil,python with none. However, in JavaScript, there is also a null-like undefined, which represents "undefined." The designer of JavaScript wants NULL to represent an empty value, while undefined indicates that the value is undefined. It turns out that there is no such thing as an egg, and the difference between the two is insignificant. In most cases, we should all use NULL. Undefined is useful only when judging whether a function parameter is passed or not.  3. Array 3.1 declares an array: the array is represented by [], separated by elements. Or: New Array (1, 2, 3, "Hello"); 3.2 Change ArrayLength: var arr = [1, 2, 3];arr.length;//this time the length is 3arr.legth = 7;//at this time the length becomes 7, of which 4 are undefined; 3.3 array of common methods: IndexOf ();//find subscript for elements in an array slice ();//Use the subscript value to intercept the parameters in the array to return the new array, do not enter parameters to return all push ();//Add several variables after the array pops ();//delete the last element of the array unshift ();//add Element Shift () to the front of the array;// Delete the first element of the array sort ()://Sort array reverse ();//reverse Array order Splice (0, 1, "good", "food");//Follow the subscript value and delete the element (only lose the subscript value is deleted) concat ();//merge array, similar to " + "Use Join (" * "); object 4.1JavaScript is a set of unordered collections consisting of key-values, for example: var person = {name: ' Bob ', age:20, tags,   4. : [' JS ', ' web ', ' mobile '], City: ' Beijing ', Hascar:true, zipcode:null}; The key of a JavaScript object is a string type, and the value can be any data type. The person object above defines a total of 6 key-value pairs, each of which is also known as the object's properties, for example, the person's Name property is ' Bob ' and the ZipCode property is null. To get the properties of an object, we use the object variable. How to Name the property: Person.name; ' Bob ' Person.zipcode; The null 4.2in operator "name" in person;//determines whether the name exists in the person Trueperson.hasownproperty ("name"); Determines whether the Name property is own (non-inherited) true5. Variable JS is a dynamic language and can be compatible with multiple types: var a = 10.0;a = "Hello";  6.strict mode (global variable declaration) does not declare a variable type, or a global variable, such as: i = 15; Declare "use strict" in the first line of the code to enter strict mode, and strict mode is prohibited from declaring variable types directly;  7. Stitching string: 1. The "+" sign can be used in the general case 2. Too many characters can use the ES6 templateString Mode: Example: var name = ' Xiao Ming '; var age = 20; var message = ' Hello, ${name}, you ${age} year old! '; alert (message);  8. String, 8.1 Gets the string length: var s = ' Hello, world! '; S.length; 138.2 using an array subscript (index) to take a value: var s = ' Hello, world! '; S[0]; ' H ' s[6]; "S[7"; ' W ' s[12]; '! ' s[13]; Undefined indexes that are out of range do not give an error, but return undefined note: Assigning a value using a string index is not allowed; continued: s[7] = "Q";//Content or W, does not change; 8.3JavaScript provides some common methods for strings, Note that calling these methods does not alter the contents of the original string, but instead returns a new string: Touppercasetouppercase () capitalizes a string: var s = ' Hello '; S.touppercase (); Returns ' Hello ' Tolowercasetolowercase () turns a string all lowercase: var s = ' Hello '; var lower = S.tolowercase (); Return ' Hello ' and assign value to variable lower lower; ' Hello ' indexofindexof () will search for the location where the specified string appears: var s = ' Hello, world '; S.indexof (' World '); Returns 7 S.indexof (' World '); The specified substring is not found and returns -1substringsubstring () returns the substring of the specified index interval: var s = ' Hello, world ' s.substring (0, 5); Starting from index 0 to 5 (excluding 5), return ' Hello ' s.substring (7); Starting from index 7 to the end, return to ' World '  9.for...in ... Iterate over an array (like Java enhanced for loop) var o = {name: ' Jack ', age:20, City: ' Beijing '}; for (var key in O) {alert (Key); ' Name ', ' age ', ' city '} 10. Data structure Map and set (both cannot use subscript traversal) data structure set do not repeat Key10.1map method: var m = new Map ([[' Michael ', 95], [' Bob ', M.set], [' Tracy ', []]]; ("Dog", 88);//add Element M.get (' dog '); 88 10.2set method: "Use Strict" var m = new Set ([5]; M.delete (1);//delete 1 This element  10.3map and Set traversal using for ... of ... Traverse Set:var m = new Set ([+ +]); for (var k of M) {alert (k);} Map:var m = new Map ([[' Michael ', ' ['], ' [' Bob ', ' ['], ' ['] ['] ['] ', ' ['] ' [']] ', ' [' Mike ', ' +] '); M.set ("Dog", 88); 88for (var k of M) {alert (k);} 10.4for...in ... and For...of ... The difference between var a = [' A ', ' B ', ' C ']; A.name = ' Hello '; for (Var x in a) {alert (x);//' 0 ', ' 1 ', ' 2 ', ' name '}var a = [' A ', ' B ', ' C ']; A.name = ' Hello '; for (var x of a) {alert (x);//' A ', ' B ', ' C '}    

1.js Basic Syntax

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.