I'm very new to JavaScript (and coding) and I'm studying through the book Head First JavaScript. From reading, I thought an anonymous function could be an argument since functions are values, but I think I might have this wrong.
The following code, according to what I thought, should output 7
. Instead it outputs, function () { return (m * n); }1
I've done similar things with strings and they don't process as expected either, according to my presumptions.
Please tell me where I have gone wrong in my understanding of anonymous functions, the limits in the use of anonymous functions in javascript, and why we want to use anonymous functions ever, in general. Thanks so much.
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<p id="demo"></p>
<script>
function addIt(x, y) {
return (x + y);
}
var m = 2;
var n = 3;
var total = addIt(function() { return (m * n); }, 1);
document.getElementById("demo").innerHTML = total;
</script>
</body>
</html>