How to understand the function concept in javascript

王林
Release: 2020-05-28 16:54:48
Original
2625 people have browsed it

How to understand the function concept in javascript

What is a function?

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

Let’s give a small example:

How to understand the function concept in javascript

When we click the button, a warning box as shown below will pop up:

How to understand the function concept in javascript

Grammar format:

function functionname() { // 执行代码 }
Copy after login

function is the keyword used to define a function.

Note: JavaScript is case-sensitive. The keyword function must be lowercase, and the function must be called with the same case as the function name.

Function with parameters

Syntax format:

function myFunction(var1,var2) { 代码 }
Copy after login

Example:

How to understand the function concept in javascript

After clicking, it will display:

How to understand the function concept in javascript

Parameters with return values

The return value is obtained through the return statement accomplish.

Syntax format:

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

Note: Using the return statement does not mean that the entire JavaScript stops execution, just the function. JavaScript will continue executing code from where the function was called.

Example:

How to understand the function concept in javascript

Recommended tutorial:js introductory tutorial

The above is the detailed content of How to understand the function concept in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!