JS Basic syntax

Source: Internet
Author: User

7.number &&Data conversion to number type initialization:varNumber =NewNumber (' 23 ');//use the constructor to create * use the TypeOf operator to return ' object '; Use the type function to return ' number ' ;      varNumber = 23;//Direct Volumemethods: number.tofixed ();//keep several decimals, the result will be rounded; for example: 1.0263.toFixed (2); The result is "1.03";(8). toString (n); convert to n-binary string; 8..  ToString (); Conversion type: There are three ways to convert the other to numbers: number (value)&& +value;      Returns the value directly for the number type; is a Boolean type,true->1;false->0;        Is null, returns 0;      For undefined, return nan;        Is string: An empty string returns 0;        If there is only a number in the string (which can contain a leading symbol), it is converted directly to a number and ignored if there is a leading zero; If the string contains a floating-point format (2.3), the rules are as above; If the string contains 16 binary formats (0xf), it is converted to a hexadecimal integer value of the same size;        Returns NaN if the string contains other format characters;   If it is an object, then the valueof () method is called, the return value is converted according to the above rule, and if it is Nan, the ToString () method is called, which is converted according to the preceding rule; Paseint (value)&&parsefloat (Value,system);        Converts the value of system binary to a number of 10 binary; When an empty string returns Nan the first character is not a number or a sign, and the first character of Nan returns a number when the second is processed, until it is not a number;' 152abc,sda12 ' = 152do not consider the decimal point to be a valid character, for example:' 10.12 ' = 108.string &&convert to a string type, and the action string creates strings:varString =NewString (' Alibaba ');//constructor function;      varstring = ' Alibaba ';//Direct volume, quotation marks can be double and single-lead;      varstring = ' ali\ ' Baba '; = Ali ' Baba 'be escaped; varstring = ' non-printable character: \ n, \ r, \ t '; varstring = ' using hexadecimal notation, \x + nn: \x41 '; varString = ' \u + nnnn: \u0061 '; using Unicode notation;    Convert to String: string (value); "+value;  (number). toString (sys); A string converted to a sys binary; Properties and methods of string: Length=length; String[n]= = Access to the string inside of section n+1 characters; Concat ()= = Merge string generally directly with +; Replace (key, value)=replacing a key with a value,key can be a regular expression; IndexOf (value)= = starts by looking for value and returns the subscript of the first character of value;1 were not found; LastIndexOf (value)= = Starts from the back to find and returns the subscript of the first character of value;1 were not found; Search ()= = finds value and returns the subscript of the first character of value;1 were not found; Match (value)=returns the [value] length of 1 if the argument is a string, or returns an array of all matching elements if the regular expression; Slice ()/substing () \substr () = intercepts a string, receives two parameters begin,end (optional, not truncated to the last character); Returns a string containing a begin subscript that does not contain an end subscript; The parameter can be a negative number; Split () =use characters or regexp to cut strings for parameters and generate arrays; toUpperCase ()=converts lowercase letters to uppercase; toLowerCase ()=converts capital letters to lowercase letters; CharAt (number)=returns the character of the number position; charCodeAt (number)=The Unicode encoding of the character that returns the number position; String.fromCharCode (Unicode)=a static method that returns Unicode-corresponding characters; Trim ()=remove spaces at both ends of the string; Trimleft ()=Remove the space to the left of the string; TrimRight ()=Remove the space to the right of the string; StartsWith (str)=determines whether a string begins with Str endsWith ()=determines whether the string ends with str repeat (number)=tiles a string number times; Contains (val)= = Determines whether the string contains Val (); The lookup method can be used instead, as long as the returned value is not-1, that is, for inclusion; Codepointat (sub)=returns the encoding of the character corresponding to the sub, which supports more characters than charCodeAt (); String.formcodepoint (val)=a static method that returns the corresponding character of the encoding;9.Blooean &&Convert to Blooean type creation:varFlag =NewBlooean (true); construction function; varFlag =true; direct quantity;      Convert to Boolean type method: Boolean (val); !!Val (); if(val) =the Val is implicitly converted;10.Array &&the creation and operation of the array is created:varArray =NewArray (Val); Example:varArray =NewArray (2); Create an array of length 2;    [Undefined,undefied]; varArray =NewArray (2, 3); = = [2, 3]; varArray =NewArray (' abc ', ' Def ', ' 456 '); = = [' abc ', ' Def ', ' 456 ']; varArray = [2, 3, ' abc '] = =Direct volume; varArray = ' a,b,c,d '. Split (', '); Properties and methods of the array: [].length= = length, not a read-only attribute;vararr = [A, b, c]; Arr.length = 2; = Arr =[A, b]; Push (); + = Inserts an element from the end of the array;varA = [1, 2, 3].pop (); = a =3; Unshift (); + = Inserts an element from the head of the array;vararr = [1, 2, 3]; Arr.push (4, 5, 6); = arr = [1, 2, 3, 4, 5, 6]; Pop (); = = Delete and return the last element of the array; vr = []; Arr.unshift (1, 2, 3) = arr = [1, 2, 3]; Shift (); + = Deletes and returns the first element of the array;varA = [1, 2, 3].shift (); + A = 1; Slice (); = = Intercept array similar to string;vararr = [1, 2, 3, 4, 5],arr1 = Arr.slice (2,-1); = = ARR1 = [3, 4];arr not change; Splice (); =vararr = [1, 2, 3]; Arr.splice (1, 1, ' A ', ' C '); = = Arr = [1, "A", "C", 3]; Concat (Val); =generate a new array, get the values of two arrays (also String,obj,number), and place the values of the parameters at the end of the array; Join (Val); = = Converts the array to a Val-separated string;vararr = [1, 2, 3]; Arr.join ("); "123"; Arr.join ('); 1 2 3 "; Reverves (); =reverses the order of the array; Sort (); =sort an array; You can receive a function as a parameter;    IndexOf (); LastIndexof (); = = A string-like lookup method; Returns the position of the element in the array;1 indicates that there is no such element in the array; Array.isarray (); =judgment is not an array; similar to IsNaN (); New method: ForEach (); =loop Array; Every (func)=whether the array satisfies the Func condition; returns False if one is not satisfied; Some ()= return as soon as one satisfiestrue; Map ()=generates a new array based on the current array; Filter ()=returns a new array that satisfies the condition; Reduce ()=An array can be merged to set the starting value; Reduceright ()=>11.object &&creation and operation; Object Category: Math=the object of a mathematical operation; Date.=Date object; Fucntion=function object; All functions can be regarded as an instance of it; Object=the source of all objects; all things are objects; REGEXP=Regular Expressions; Host object: Window Top=the iframe accesses the external window object; Location History Custom Objects:varobj = {}; Initialize:varobj =NewObject (); varobj = {};    The properties and methods of the object; Constructor =pointers; functionPerson () {}; varper = Person.prototype;//Obtain the prototype of the object;        vararr = Per.constructor;//prototype Access objects;arr =functionPerson () {};    ToString ();    ValueOf (); hasOwnProperty (key); =identify attributes or methods that are themselves rather than inherited attributes;12.  Math object; Math.PI=π; Math.sqrt2=Radical 2; Math.Abs ()=absolute Value; Math.random ()= = Returns a 0 ~1 of random numbers;  Math.max () \math.min (); MATH.AQRT ()=calculates the square root; MATH.PSW (i, j); =calculate the J-th square of I; Math.floor (); = Math.floor (1.5) + 1; Math.ceil (); = Math.ceil (1.2) + 2; Math.Round (); = Math.Round (1.2) + 1; Math.Round (1.5) = 2; Math.sin ()=returns the sine of the specified number of degrees; Math.Cos ()=returns the cosine of the specified number of degrees; Math.atan2 ()=returns the angle of the specified number of tangent values;13.  URL; Methods: encodeURI (); =encode the entire URL; decodeURI (); =decoding the code on the upstairs; encodeURIComponent ();//encode the back part of the.decodeURIComponent () =decoding the code on the upstairs;14.  Date object; Initialize:varDate =NewDate (); varDate =NewDate (' 2014/10/01 ')); Properties and Methods: toUTCString ()=converts a Date object to a string based on Universal Time (UTC) and returns the result; getFullYear () \setfullyear (); =get year \ Set year GetMonth () \setmonth ();=month GetDay () \setday ();=Zhou GetDate () \setdate ();=days GetHours () \getminutes () \getseconds () \sethours () \setminutes () \setseconds ();=Time Division seconds GetTime () \settime ();= = UTC time milliseconds; from 1970/1/1 to 0:00;

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.