java script return code example

Example 1: define function in javascript

/* Declare function */
function myFunc(param) {
  // Statements 
}

Example 2: jquery function return

function myFunction(p1, p2) {
  var a = "";
 $.post( "/url", function( data ) {a = data.result;});
  return a;
}

Example 3: jquery function return

function myFunction(p1, p2) {
  var a = "";
  $.ajax({
    method: "POST",
    url: "/url",
    data: {p1: p1,p2: p2},
    success: function (data) {a = data.result;},
    error: function (data) {a = data.result;}
    });
  return a;
}

Example 4: return statement javascript

function test(arg){
    return arg;
}

Example 5: return string from javascript function

<script>
function showName() {
  var result;
  result = addString('Hello', ' World');
  document.write (result );
}

// in below we will check how to return string from function
function addString(fName, lName) {
  var val;
  val = fName + lName;
  return val;  // returning string from function
}
</script>
<input type = "button" onclick = "showName()" value = "Result">
  
/*
I hope it will help you.
Namaste
Stay Home Stay Safe
*/

Example 6: return this javascript

Function.prototype.method = function (name, func) {
  this.prototype[name] = func;
  return this;
};