JavaScript Learning Notes (7)--javascript syntax function

Source: Internet
Author: User

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

function functionname () {  // Here is the code to execute }

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.

Tip: JavaScript is case sensitive. The keyword function must be lowercase and must be called with the same casing as the function name.

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 (,):

MyFunction (Argument1,argument2)

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

function myFunction (var1,var2) {  // Here is the code to execute }

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

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.

function myFunction () {  var x=5; return x;}

The above function returns the value 5.

Note: The entire JavaScript does not stop executing, just the function. JavaScript will continue to execute code from where the function is called.

JavaScript Learning Notes (7)--javascript syntax function

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.