title: JavaScript: When assigning an anonymous function to a variable, function return value is not passed, rather the function as a string
body: <p>I am trying to learn JavaScript but I've come across a hurdle. If the answer is obvious and reachable through a simple search I apologize in advance. I am a novice to programming and JavaScript, and unsure what line of inquiry to follow.</p>
  
  <p>In the following code, the function takes values from a HTML form, does some processing and sends them back. I've tested the input and output process and it's working correctly.</p>
  
  <pre><code>function foo() {
  
  var x = parseInt(document.formdata.fieldone.value);
  var y = parseFloat(document.formdata.fieldtwo.value);
  
  if (isNaN(y))
      { var z = x; }
  else
      { var z = function(x, y) {
              if ((y * (x / 100)) &lt; 1) {
                  return (x + Math.ceil(y * (x / 100))); }
              else if ((y * (x / 100)) &gt; 1) {
                  return (x + Math.round(y * (x / 100))); }
              else {
                  return 0; } } }
  
  var bar = document.getElementById("output");
  
  bar.innerHTML = z; }
  </code></pre>
  
  <p>The problem is, when the else branch of the conditional statement tries to process the anonymous function, the return value isn't assigned; rather the entirety of the function as a string. That is, the following appears in the HTML page:</p>
  
  <blockquote>
    <p>function (x, y) { if ((y * (x / 100)) &lt; 1) { return (x + Math.ceil(y * (x / 100))); } else if ((y * (x / 100)) > 1) { return (x + Math.round(y * (x / 100))); } else { return 0; } }</p>
  </blockquote>
  
  <p>I've tested the code in Chrome and Firefox and the result is the same.</p>
  
  <p>Any help is appreciated and thank you in advance.</p>