why when I put a function as an array value, it returns function syntax and not its value?

-1
var A = [ 'dog', 'cat', function () { return 2 * 2 } ] ;

document.body.innerHTML = A[2]

Return this: function () {return 2 * 2} Obviously I want you to return 4 , not the syntax of a function Or is it that you can not put a function in an array?

    
asked by lalo2019 02.01.2019 в 17:58
source

1 answer

1

Returns what you ask. The function is in the array and the function is returned to you. If what you want is the result of executing the function, you have to ask for it.

document.body.innerHTML = A[2]();

If A[2] is a function, then A[2]() is a call to that function and you will get the value returned by it.

    
answered by 02.01.2019 / 18:14
source