JavaScript functions recognize, common functions in JS

Source: Internet
Author: User
Tags square root

JavaScript functions:

Also called a method, used to store a piece of code that is called when needed.

A function is an event-driven or reusable block of code that executes when it is invoked.

Functions need to contain four elements: return type, function name, argument list, function body

Extension: A function of a strongly typed language

 Public int Sun (int A,int  b) {    return = a +b;}

return returns, Sun function name, int a,int B, argument list, int integer.

function with no return value:

 Public void Sun (int A,int  b) {}sun;

Such a parameter, a B is a formal parameter, that is, the formal parameters, the calling function is given the parameter is the argument, that is, the actual parameters.

function definitions in javascript:

    // defining function Jisun    function Jisuan () {    alert ("This is the function Jisuan")    ;    } // calling functions    Jisuan ();

function is the definition of the functions and does not execute when the function is called to look for the definition of the function name.

The definition of functions in JavaScript and the order in which they are called can be written in the write definition.

Functions with Parameters:

    // function    with parameters function Jisuan (b) {        alert (a+b);    }     // calling functions    Jisuan (3,5);

It is important to note that defining a function is a parameter that does not need to be defined with Var.

Functions with return values:

    function Jisuan (b) {        return A +b    ;    } // calling functions    var c=jisuan (3,5);    alert (c);

The return value is returned to the calling function, which typically defines a variable that assigns the return value to the variable.

Add: A function with default values in a strongly typed language, JS does not support functions with default values

    function Jisuan (a,b=2) {        alert (a+b);    }     // calling functions    Jisuan (3);
common functions in javascript:

document.write (""); Output statement

math.random (); Get a random number between 0-1

    document.write (Math.random ());

    document.write (parseint (Math.random () *10));

Date Time class function:
    // get current time    document.write (Date ());

    // get current time    var d=New  Date ();     // gets the current time stamp    document.write (D.gettime ());

    // get current time    var d=New  Date ();     // get current year    document.write (D.getfullyear ());
    // get current time    var d=New  Date ();     // to get the current month, note that this requires +1    document.write (D.getmonth () +1);
    // get current time    var d=New  Date ();     // get the current date    document.write (D.getdate ());
    // get current time    var d=New  Date ();     // get current when    document.write (D.gethours ());
    // get current time    var d=New  Date ();     // gets the current fraction    document.write (D.getminutes ());
    // get current time    var d=New  Date ();     // gets the current few seconds    document.write (D.getseconds ());
    // get current time    var d=New  Date ();     // gets the current day of the week    document.write (D.getday ());
    // get current time    var d=New  Date ();     // gets the current few milliseconds    document.write (D.getmilliseconds ());
Math class functions:
    // Rounding up    document.write (Math.ceil (3.5));

    // Rounding down    document.write (Math.floor (3.5));

    // take absolute value    document.write (Math.Abs ( -2));     // Rounding    document.write (Math.Round (5.5));     // returns the highest value    document.write (Math.max (5,7));     // returns the lowest value    document.write (Math.Round (5.7));     // returns the Power    of a two-digit number document.write (Math.pow (5.7));     // returns the square root    document.write (Sqrt.round (5));
String Functions:
    var str= "Hello World";     var s= "L";     // returns the position of the first occurrence of a character in a string     document.write (Str.indexof (s));     // returns the character    at the specified position document.write (Str.charat (0));     // returns the position of the last occurrence of a character in a string     document.write (Str.lastindexof (s));     // Intercept string    document.write (str.substring (1,3));     // intercept string corresponding length    document.write (Str.substr (1,3));

    var str= "Hello World";     // replace the corresponding string    Str=str.replace ("Hell", "^^");    document.write (str);

    var str= "Hello World";     // Replace all corresponding strings    Str=str.replace (/l/g, "^^");    document.write (str);

    // Split splits a string into a string array by dividing the string into substrings.     var str= "Hello World";     var arr=str.split ("");

such as the string "Helllo world" will be broken into an array of spaces, the first value Hello, the second value of world

Other:

Length Property
Returns the length of a string, which refers to the number of characters it contains.

toLowerCase
Turns the entire string into lowercase letters.
var lower_string = A.tolowercase ();
lower_string = "Hello"

toUpperCase
Turns the entire string into uppercase.
var upper_string = A.touppercase ();
upper_string = "HELLO"

Search
Performs a regular expression matching lookup. If the lookup succeeds, returns the matching index value in the string. otherwise returns-1.
var index1 = A.search (re);
index1 = 0
var index2 = B.search (re);
Index2 =-1

Add:

Naming conventions for variable names: Usually beginning with letters, usually with lowercase letters, as far as possible without special symbols

Naming conventions for function names: Hump method, first letter lowercase, first letter of each other word capitalized

JavaScript functions recognize, common functions in JS

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.