Home > Web Front-end > JS Tutorial > A brief discussion on the return statement in javascript_javascript skills

A brief discussion on the return statement in javascript_javascript skills

WBOY
Release: 2016-05-16 15:50:21
Original
1031 people have browsed it

The return statement is very important in js. It not only has the function of returning function values, but also has some special usages. It is very necessary to have a clear grasp. Let's briefly introduce the function of the return statement with examples.

1. Used to return control and function results:

Usually, the return statement is necessary for a function, because it is often required that the function will get an expected return value after a series of code executions, and this value is returned through the return statement, and control is returned to the calling function.

Grammar format:

return expression

The code example is as follows:

function add(){
 var a=1;
 var b=2;
 return a+b;
}
function func(){
 console.log(add())
}
func();

Copy after login

In the above code, when the func() function is called, the control is held by the func function. When the add function is called again, the control is handed over to the add function, and then a value is returned and the control is handed over to func. function.
Usually return is followed by an expression, but it is not absolute, for example:

return;

In this case, the control is simply transferred to the calling function to continue execution.

Extended description:

There is nothing special about the ordinary application of the return statement. The most important thing to note is the use of return false. The event handler function returns false to prevent the occurrence of the default event.
The code example is as follows:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset=" utf-8"> 
<title>脚本之家</title> 
<script type="text/javascript"> 
window.onload=function(){ 
 var olink=document.getElementById("thelink"); 
 olink.onclick=function(){
  return false
 } 
} 
</script> 
</head> 
<body> 
<a href="" id="thelink">脚本之家</a> 
</body> 
</html>

Copy after login

Clicking a link will cause an onclick event. Its default action is to point the link to the link specified by the href attribute. However, if the event handler uses return false, the default event will be prevented from occurring.
Return false can also prevent event bubbling from occurring.

The above is the entire content of this article, I hope you all like it.

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template