When and why to ‘return false’ in JavaScript?

14 Apr, 2020
Return statements, in programming languages, are used to skip the currently executing function and return to the caller function. Return statements may/may not return any value. Below is the example of a return statement in JavaScript.
Example 1:// The return statement returns// the product of 'a' and 'b'function myFunction(a, b) {  return a * b;   }
// The return statement returns// the product of 'a' and 'b'function myFunction(a, b) {  return a * b;   }
Example 2: Similarly, we can return true or false in a simple JavaScript function.function isEqual(num1, num2) {    if (num1 == num2) {        return true;    } else {        return false;    }}
function isEqual(num1, num2) {    if (num1 == num2) {        return true;    } else {        return false;    }}
These were some of the basic implementations of return false statement in JavaScript. However, there exist some more interesting implementations. Web Developers use ‘return false’ in different ways. During form submission, if a particular entry is unfilled, return false is used to prevent the submission of the form.
Below example illustrate the approach:
Example:<!DOCTYPE html><html>  <head>    <title>        Return false    </title>    <script>        function validateForm() {            var x = document.forms["myForm"]["fname"].value;            if (x == "") {                alert("Please fill all the entries");                return false;            }        }    </script></head>  <body style="text-align:center;">    <h1 style="color: green;">             GeeksforGeeks     </h1>    <form name="myForm"           action="/action_page.php"           onsubmit="return validateForm()"           method="post">        Name:        <input type="text" name="fname">        <input type="submit" value="Submit">    </form></body>  </html>
<!DOCTYPE html><html>  <head>    <title>        Return false    </title>    <script>        function validateForm() {            var x = document.forms["myForm"]["fname"].value;            if (x == "") {                alert("Please fill all the entries");                return false;            }        }    </script></head>  <body style="text-align:center;">    <h1 style="color: green;">             GeeksforGeeks     </h1>    <form name="myForm"           action="/action_page.php"           onsubmit="return validateForm()"           method="post">        Name:        <input type="text" name="fname">        <input type="submit" value="Submit">    </form></body>  </html>
Output:
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
How to append HTML code to a div using JavaScript ?
Difference Between PUT and PATCH Request
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?