Deep understanding of JavaScript built-in Functions _ Basics

Source: Internet
Author: User
Tags abs floor function getdate join local time natural logarithm square root javascript array

JavaScript functions can be divided into five categories altogether:

· General functions

· Array functions

· Date function

· Mathematical functions

· String functions

1. General functions

The 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: Test is (true) No (false) is not a number.

(6) parsefloat function: Converts a string into a character point number form.

(7) parseint function: Converts a string into an integer number form (you can specify a few).

(8) Prompt function: Displays an input dialog box that prompts for user input. For example:

<script language= "JavaScript" >
<!--
alert ("Input error");
Prompt ("Please enter your name", "name");//(title, preset)
confirm ("OK!"). ");
-->
</script>

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

2. Array function

The JavaScript array functions include the following 4 functions:

(1) Join function: Converts and joins all elements in an array into a string. Cases:

function Joindemo ()
{
var a, b;
a = new Array (0,1,2,3,4);
B = A.join ("-");//separator
return (b); Back to the b== "0-1-2-3-4"

(2) Langth function: Returns the length of an array. Cases:

function Lengthdemo ()
{
var a, l;
a = new Array (0,1,2,3,4);
L = a.length; return
(L);//l==5
   }

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

function Reversedemo ()
{
var a, l;
A = new Array (0,1,2,3,4);
L = A.reverse ();
return (L);

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

function Sortdemo ()
{
var a, l;
a = new Array ("X", "Y", "D", "Z", "V", "M", "R");
L = A.sort ();
Retu RN (l);
} 

3. Date function

The JavaScript date function includes the following 20 functions:

(1) getdate function: Returns the "Day" section of the date, and the value is 1~31. Cases:

function Datedemo ()
{
var d, s = "Today's" date is: ";
D = new Date ();
S + + (D.getmonth () + 1) + "/";
S + + d.getdate () + "/";
S + + d.getyear ();
return (s);
} 

(2) Getday function: Returns the day of the week, the value is 0~6, of which 0 means Sunday, 1 means Monday, ..., 6 means Saturday. Cases:

function Datedemo ()
{
var d, Day, x, S = ' today is: ';
var x = new Array ("Sunday", "Monday", "Tuesday");
var x = X.concat ("Wednesday", "Thursday", "Friday");
var x = x.concat ("Saturday");
D = new Date ();
Day = D.getday ();
Return (s + + x[day]);

(3) gethours function: Returns the "Hour" section of the date, and the value is 0~23. Cases.

function Timedemo ()
{
var d, s = "The current local time is:";
var c = ":";
D = new Date ();
S + + d.gethours () + C;
S + + d.getminutes () + C;
S + + d.getseconds () + C;
S + + d.getmilliseconds ();
return (s);
}

(4) Getminutes function: Returns the "Minutes" section of the date, and the value is 0~59. See the above example.

(5) GetMonth function: Returns the "month" section of the date, and the value is 0~11. Of these, 0 said January, 2 means March, ..., 11 means December. See the previous example.

(6) Getseconds function: Returns the "Seconds" section of the date, and the value is 0~59. See the previous example.

(7) GetTime function: Returns the system time.

function Gettimetest ()
{
var d, S, T;
var Minmilli = 1000 *;
var Hrmilli = Minmilli *;
var Dymilli = Hrmilli *;
D = new Date ();
t = D.gettime ();
s = "It ' s been"
s + = Math.Round (T/dymilli) + "days since 1/1/70";
return (s);

(8) getTimezoneOffset function: Returns the time difference for this region (local times and GMT Greenwich Mean time interval), in minutes.

function Tzdemo ()
{
var d, tz, S = "The current local time is";
D = new Date ();
TZ = D.gettimezoneoffset ();
if (TZ < 0)
S + + tz/60 + "hours before GMT";
else if (TZ = = 0)
s + + "GMT";
else
S + + tz/60 + "hours after GMT";
return (s);

(9) GetYear function: Returns the "Year" section of the date. The return value is based on a 1900-year base, for example, 1999 is 99. There are examples ahead.

(a) Parse function: Returns the number of milliseconds (local time) from January 1, 1970 zero.

function Gettimetest (testdate)
{
var d, S, T;
var Minmilli = 1000 *;
var Hrmilli = Minmilli *;
var Dymilli = Hrmilli *;
D = new Date ();
t = Date.parse (testdate);
s = "There are"
s + + math.round (Math.Abs (T/dymilli)) + "days" s + + "
between" + TestDate + "and 1/1/70"; 
   return (s);
}

(one) setdate function: Set the date of the "Day" section, the value is 0~31.

sethours function: The "hour" section of the set date, and the value is 0~23.

(setminutes function: The "Minutes" section of the set date, and the value is 0~59.)

setmonth function: The "month" section of the set date, and the value is 0~11. Of these, 0 said January, ..., 11 means December.

setseconds function: The "Seconds" section of the set date, and the value is 0~59.

(settime) Function: Set time. The time value is the number of milliseconds from the whole of January 1, 1970 zero.

(setyear) function: The "Year" section of the set date.

(togmtstring) Function: convert date to string, GMT Greenwich Standard Time.

setlocalestring function: The conversion date becomes a string, local time.

(20) UTC function: Returns the number of milliseconds from the January 1, 1970 zero, calculated as GMT Greenwich Mean time.

4. Mathematical Functions

The JavaScript math function is actually the math object, which includes two parts: attributes and Functions (or methods). Among them, the attributes mainly have 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 value of 1/2), Math.sqrt2 (square root of 2).

The function has the following 18:

(1) ABS function: That is, Math.Abs (in the same below), returns the absolute value of a number.

(2) ACOs function: Returns the inverse cosine of a number, resulting in 0~π radians (radians).

(3) ASIN function: Returns the inverse string value of a number, the result is-Π/2~Π/2 radians.

(4) Atan function: Returns the tangent value of a number, the result is-Π/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, the result is -1~1.

(8) Exp function: Returns the exponentiation value of E (natural logarithm).

(9) Floor function: Returns the maximum integer value of a number (less than or equal to).

Log function: A natural logarithmic 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's exponentiation.

(random) Function: Returns a random number of 0~1.

(round) Function: Returns the rounded value of a number, which is an integer.

Sin function: Returns the sine of a number, and the result is -1~1.

(sqrt) function: Returns the square root value of a number.

Tan function: Returns the tangent value of a number.

5. String function

The JavaScript string function completes the culture of the font size, color, length, and lookup of the string, including the following 20 functions:

(1) Anchor function: Create a link point (anchor) for use as a hyperlink. The Anchor function sets the name of the chain point of the <a name...>, and another function link sets the URL address of the <a href=...>.

(2) Big function: Add the font to a number, the same as the <BIG>...</BIG> label results.

(3) BLINK function: Make string flicker, same as <BLINK>...</BLINK> label result.

(4) Bold function: Make the font bold, the same as the <B>...</B> label results.

(5) Charat function: Returns a character specified in the string.

(6) Fixed function: Set the font to a fixed-width font, the same as the <TT>...</TT> label results.

(7) FontColor function: Set the font color, the same as the <font color=color> label results.

(8) FontSize function: Set the font size, the same as the <font size=n> label results.

(9) IndexOf function: Returns the first found subscript index in the string, starting from the left.

(Ten) italics function: Make font italic, same as <I>...</I> label result.

(one) LastIndexOf function: Returns the first found subscript index in the string, starting from the right.

() Length function: Returns the length of a string. (without parentheses)

Link function: Generates a hyperlink that is equivalent to setting the URL address of the <a href=...>.

(SMALL) function: Reduce the font by one number, the same as the <SMALL>...</SMALL> label results.

STRIKE function: Add a horizontal line to the middle of the text, the same as the <STRIKE>...</STRIKE> label result.

Sub function: Displays a string as the subscript (subscript).

(substring) Function: Returns a number of characters specified in a string.

(a) SUP function: Display string as superscript (superscript).

(tolowercase) Function: Converts a string to lowercase.

toUpperCase function: Converts a string to uppercase.

The above in-depth understanding of the JavaScript built-in function is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.