JavaScript Function Learning 3

Source: Internet
Author: User

JavaScript Functions

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

JavaScript function syntax

A function is a block of code wrapped in curly braces, preceded by a keyword function:

When the function is called, the code inside the function is executed.

A function can be called directly when an event occurs (such as when a user taps a button) and can be called from anywhere by JavaScript.

Calling a function with parameters

When you call a function, you can pass a value to it, which is called a parameter.

These parameters can be used in functions.

You can send any number of arguments separated by commas (,):

When you declare a function, declare the argument as a variable:

Variables and parameters must appear in a consistent order. The first variable is the given value of the first parameter passed, and so on.

function with return value

Sometimes, we want the function to return the value to the place where it was called.

This can be achieved by using the return statement.

When you use the return statement, the function stops execution and returns the specified value.

Because local variables are only used within functions, different functions can use variables of the same name.

Local variables are created when the function starts executing, and local variables are automatically destroyed when the function finishes executing.
If a variable is not declared within a function (without using the var keyword), the variable is a global variable.

The JavaScript variable life cycle is initialized when it declares.

Local variables are destroyed after the function has finished executing.

Global variables are destroyed after the page is closed.

JavaScript Function Learning 3 (RPM)

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.