mohsen.noor mohsen.noor - 3 months ago 15
Javascript Question

Wrong output in Javascript calling function

I'm trying to print the result of a JavaScript function, but instead I get the whole function

Output

x7: function
function () { return "hello this is form function"; }


I want the output:
hello this is form function


HTML

<p id="demo"></p>


JavaScript

var x7 = function (){
return "hello this is form function";
};
document.getElementById("demo").innerHTML = "x7: " + typeof x7 + "<br>"+x7+"<br>";

Answer

You have to use parentheses to call your function: x7():

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>

var x7 = function (){
   return "hello this is form function";
};

document.getElementById("demo").innerHTML =

"x7: " + typeof x7 + "<br>"+
x7()+"<br>";
</script>

</body>
</html>