Java Script Basics (three) functions

Source: Internet
Author: User

Java Script Basics (three) functions

First, the functions in JavaScript

In JavaScript, a function is similar to a method in Java, a block of code that performs a specific function and can be called repeatedly. There are two types of functions in JavaScript, one is a system function and the other is a custom function.

  1. System functions

System functions are provided by JavaScript and can be called directly, and commonly used system functions include:

  parseint (): Converted to an integer.

Parsefloat (): Converts to floating-point number.

IsNaN (): Determines whether the number is nonzero, returns true for non-digits, and returns false.

Eval (): Evaluates the value of an expression.

  2. Custom Functions

To use a custom function, you must first define a custom function.

Grammar:

Function name (parameter 1, parameter 2 ...) {

JavaScript statements

return value; Options available

}

Attention:

  1, function is the key word of the functions, must have.

2, the parameters in the function, do not need to use the type, directly use the variable name.

3. The return value in the function is optional, not mandatory, and the return type is determined by the value returned.

Example:

Define a function that calculates the sum of two numbers of functions  Add (num1,num2) {     return num1 + num2;}    

  3. Function call

There are two ways to call a function:

1. Event name = function name (parameter value), for example:

onclick= "Add (10,20)";

2, directly using the function name (parameter value), for example:

var result = Add (10,20);

  4. Anonymous function

In fact, there is a function called anonymous function, that is, there is no function name.

1, the definition of anonymous functions:

var sum = function (num1,num2) {

return NUM1 + num2;

};

Description

  The var sum = function (num1,num2) means that a function implementation is saved in the sum variable.

{} is the implementation of the function, at the end do not forget there is another;

Java Script Basics (three) functions

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.