Summary of common JavaScript functions

Source: Internet
Author: User
Tags floor function local time mathematical functions natural logarithm square root javascript array

JavaScript functions:
• General functions
• Array functions
• Date function
• Mathematical Functions
• String Functions
. cookie function

1. General functions

JavaScript general functions include the following 9 functions:
(1) Alert function: Displays a warning dialog box, including an OK button.
(2) Confirm function: Displays a confirmation dialog box, including OK, Cancel button.
(3) Escape function: Converts a character to a Unicode code.
(4) Eval function: Evaluates the result of an expression.
(5) isNaN function: The test is (true) No (false) is not a number.
(6) parsefloat function: Converts a string into a numeric form of a character.
(7) parseint function: Converts a string into an integer number form (which can be specified as a binary).
(8) Prompt function: Displays an input dialog box prompting for user input. For example:

1 alert ("Input Error"), 2 prompt ("Please enter your name", "Name"), 3 confirm ("OK no!") ");

(9) Unescape function: Decodes characters encoded by the escape function.

(Ten) toFixed function: This function is to convert a few decimal points of checkprice.tofixed (2); convert Checkprice to two decimal places

 2. Array functions
The JavaScript array function consists of the following 4 functions:
(1) Join function: Converts and joins all elements in an array to a string. Cases:

(2) Length function: Returns the lengths of the array. Cases:

(3) Reverse function: Reverses the order of the array elements. Cases:

(4) Sort function: Reorder the array elements. Cases:

1 function Sortdemo () 2 {3 var a, l;4 a = new Array ("X", "Y", "D", "Z", "V", "M", "R"), 5 L = A.sort (); 6 return (L); 7}

(5) The push function: Adds a new element to an array and returns the new length value of the array. Cases:

1 <script type= "Text/javascript" >2     var my_array = new Array (' 1 ', ' 2 ', ' 3 ', ' 4 '); 3     My_array.push (' 5 ', ' 6 ', '    7 ', ' 8 '); 4     alert (my_array.length); Adds 4 elements to the returned 8 array 5 </script>

(6) Unshift function: Inserts the specified element into the starting position of the array and returns the array. Cases:

<script type= "Text/javascript" >    var my_array = new Array (' 1 ', ' 2 ', ' 3 ', ' 4 ');    My_array.unshift (' 0 '); Append element    alert (my_array.length) in front of array;  Back to 5</script>

(7) Pop function: Moves the last element in the array and returns the element. Cases:

1 <script type= "Text/javascript" >2     var my_array = new Array (' 1 ', ' 2 ', ' 3 ', ' 411 '); 3     alert (My_array.pop ())    ;   Returns 4114     alert (my_array.length); Back to </script>

(8) Shift function: Moves the first element in the array and returns the element. Cases:

1 <script type= "Text/javascript" >2     var my_array = new Array (' 1 ', ' 2 ', ' 3 ', ' 4 '); 5     alert (My_array.shift ())  ;   Return to     alert (my_array.length); Back to Panax Notoginseng </script>

(9) Obj.slice (Start,end) returns a segment of an array and gets a new array. Note that the end-corresponding element is not included, and if you omit end, all elements after start are copied, for example:

1 <script type= "Text/javascript" > 2     var my_array = new Array (' 1 ', ' 2 ', ' 3 ', ' 4 '); 5     var new_array = My_array.s  Lice (0,2);   Returns 0-2 elements 6     var new_array = My_array.slice (0,-1);//Returns an element other than the last 7     var new_array = my_array.slice (0); Returns all elements after the No. 0 8 for     (var i =0;i<new_array.length;i++) {9         alert (New_array[i]);     }13 </script>

(10) Obj.concat (ITEM1,ITEM2,ITEM3,ITEM4,ITEM5 ...) returns a new array that is composed of two or more arrays (which can also be a variable). Cases:

1 <script type= "Text/javascript" > 2     var my_array = new Array (' 1 ', ' 2 ', ' 3 ', ' 4 '); 3     var my_array1 = new Array (' 5 ', ' 6 '); 4     var my_array2 = new Array (' 7 ', ' 8 '); 5     var my_array3 = new Array (' 9 '); 6     var my_name = ' smartop ';    can also be a string of 7     var my_age = +            ; It can also be a number 8     var new_array = My_array.concat (my_array1,my_array2,my_array3,my_name,my_age); 9     alert (new_ Array); </script>

3. Date function
The JavaScript date function consists of the following 20 functions:
(1) getdate function: Returns the day portion of the date, with a value of 1~31. Cases:

(2) Getday function: Returns the day of the week with a value of 0~6, where 0 represents Sunday, 1 means Monday, ..., and 6 means Saturday. Cases:

(3) Gethouse function: Returns the "hour" portion of the date, with a value of 0~23. Cases.

(4) Getminutes function: Returns the "minute" part of the date with a value of 0~59. See the example above.
(5) GetMonth function: Returns the month portion of the date, with a value of 0~11. 0 means January, 2 means March, ..., and 11 means December. See the previous example.
(6) Getseconds function: Returns the "second" part of the date with a value of 0~59. See the previous example.
(7) GetTime function: Returns the system time.

(8) getTimezoneOffset function: Returns the difference in this area (local time and GMT Greenwich Mean Time), in minutes.

(9) GetYear function: Returns the "year" part of the date. The return value is based on a 1900-year base, for example, 1999 is 99. There is an example above.
Parse function: Returns the number of milliseconds (local time) that was counted from zero January 1, 1970.


(one) setdate function: The "day" portion of the set date, with a value of 0~31.
sethours function: The "hours" section of the set date, with a value of 0~23.
setminutes function: The "Minutes" section of the set date, with a value of 0~59.
Setmonth function: Sets the month portion of the date, and the value is 0~11. 0 of them represent January, ..., 11 means December.
() setseconds function: The "Seconds" section of the set date, and the value is 0~59.
() settime function: Set the time. The time value is the number of milliseconds since January 1, 1970 when the integer count is zero.
() setyear function: The "Year" section of the set date.
togmtstring function: The conversion date becomes a string, GMT Greenwich Standard Time.
() setlocalestring function: Converts the date into a string, for local time.
(20) UTC function: Returns the number of milliseconds from January 1, 1970 to the full count, measured in GMT Greenwich Mean time.

4. Mathematical Functions  
JavaScript mathematical functions are actually math objects, which include attributes and functions (or methods) in two parts. Where the attribute mainly has the following content.  
Math.e:e (natural logarithm), MATH.LN2 (natural logarithm of 2), MATH.LN10 (natural logarithm of 10), math.log2e (logarithm of E, base 2),    

MATH.LOG10E (logarithm of E, base 10), Math.PI (π), math.sqrt1_2 (square root of 1/2), math.sqrt2 (square root value of 2).
The function has the following 18:
(1) ABS function: Math.Abs (the same as below), returns the absolute value of a number.
(2) ACOs function: Returns the inverse cosine of a number with a result of 0~π radians (radians).
(3) ASIN function: Returns the inverse chord value of a number, resulting in-Π/2~Π/2 radians.
(4) Atan function: Returns the inverse tangent of a number, resulting in-Π/2~Π/2 radians.
(5) ATAN2 function: Returns the polar coordinate angle value of a coordinate.
(6) Ceil function: Returns the smallest integer value (greater than or equal to) of a number.
(7) Cos function: Returns the cosine of a number with the result of -1~1.
(8) Exp function: Returns the value of the exponent of E (natural logarithm).
(9) Floor function: Returns the maximum integer value (less than or equal to) of a number.
Log function: A natural logarithm function that returns the natural logarithm (e) value of a number.
(one) Max function: Returns the maximum value of two digits.
Min function: Returns the minimum value of two digits.
Pow function: Returns the value of a number.
Random function: Returns a 0~1 value.
Round function: Returns the rounded value of a number with an integer type.
Sin function: Returns the sine of a number with the result of -1~1.
sqrt function: Returns the square root value of a number.
Tan function: Returns the tangent of a number.

 5. String functions

(1) concat function: The text of two or more characters is combined to return a new string.

1 var a = "Hello"; 2 var b = ", World"; 3 var c = A.concat (b); 4 alert (c); 5//c = "Hello,world"

(2) IndexOf function: Returns the index (from left to right) of the first occurrence of a substring in a string. If there is no match, returns-1.

1 var index1 = A.indexof ("L"); 2//index1 = var index2 = A.indexof ("L", 3); 3 means search from the third start to the right 4//index2 = 3

(3) LastIndexOf: Returns the index (right-to-left search) at the first occurrence of a substring in a string. If there is no match, returns-1.

1 var index1 = lastIndexOf (' l '); 2//index1 = var index2 = lastIndexOf (' l ', 2)//3 means search left from the third start 4//index2 = 2

(4) CharAt: Returns a character specified in the string.

var Get_char = A.charat (0);//get_char = "H"

(4) Match: Checks a string to match a regular expression content, if there is a match return null.

1 var re = new RegExp (/^\w+$/); Storage Retrieval Mode 2 var is_alpha1 = A.match (re); 3//is_alpha1 = "Hello" 4 var is_alpha2 = B.match (re); 5//IS_ALPHA2 = null

(5) Substring: Returns a substring at the specified position in a string object.

1 var sub_string1 = a.substring (1), 2//sub_string1 = "Ello" 3 var sub_string2 = a.substring (1,4); 4//sub_string2 = "ell"

(6) Substr: Returns a substring of a string in which the incoming parameter is the starting position and length

1 var sub_string1 = a.substr (1), 2//sub_string1 = "Ello" 3 var sub_string2 = A.substr (1,4); Length 4//sub_string2 = "Ello"

(7) Replace: Used to find a string that matches a regular expression, and then use the new string instead of the matching string.

1    var r, re;                    Declares a variable. 2    var ss = "The man hits the ball with the bat.\n"; 3    SS + = "While the fielder caught the ball with the glove."; 4    re =/the/g;             Creates a regular expression pattern. 5    r = Ss.replace (Re, "A");    Replace "A" with "A".
1    var r, re;                      Declares a variable. 2    var ss = "The rain in Spain falls mainly in the plain."; 3    re =/(\s+) (\s+) (\s+)/g;        Creates a regular expression pattern. 4    r = Ss.replace (Re, "$3$2$1");   Exchange each pair of words.

(8) Search: Performs a regular expression matching lookup. If the lookup succeeds, returns the matching index value in the string. otherwise returns-1.

1 var index1 = A.search (re), 2//index1 = $ var index2 = B.search (re); 4//index2 = 1

(9) Slice: Extracts part of a string and returns a new string (same as substring).

1 var sub_string1 = A.slice (1), 2//sub_string1 = "Ello" 3 var sub_string2 = A.slice (1,4); 4//sub_string2 = "ell"

Split: A string is made into a string array by dividing the string into substrings.

1 var arr1 = A.split (""); Use the split character 2//arr1 = [h,e,l,l,o]3 length

(one) Length: Returns the length of the string, which is the number of characters it contains.

1 var len = A.length (); 2//len = 5

toLowerCase: Converts the entire string into lowercase letters.

1 var lower_string = A.tolowercase (); 2//lower_string = "Hello"

toUpperCase: Turns the entire string into uppercase.

1 var upper_string = A.touppercase (); 2//upper_string = "HELLO"

6 . Cookie function

Summary of common JavaScript functions

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.