---
layout: page
title: "JavaScript array_product function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/array_product:330
- /functions/view/array_product
- /functions/view/330
- /functions/array_product:330
- /functions/330
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's array_product

{% codeblock array/array_product.js lang:js https://raw.github.com/kvz/phpjs/master/functions/array/array_product.js raw on github %}
function array_product (input) {
  // From: http://phpjs.org/functions
  // +   original by: Waldo Malqui Silva
  // *     example 1: array_product([ 2, 4, 6, 8 ]);
  // *     returns 1: 384
  var idx = 0,
    product = 1,
    il = 0;

  if (Object.prototype.toString.call(input) !== '[object Array]') {
    return null;
  }

  il = input.length;
  while (idx < il) {
    product *= (!isNaN(input[idx]) ? input[idx] : 0);
    idx++;
  }
  return product;
}
{% endcodeblock %}

 - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/array/array_product.js)

Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are 
the closest match to this hashtable-like data structure. 

Please also note that php.js offers community built functions and goes by the 
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online 
functions that are far from perfect, in the hopes to spark better contributions. 
Do you have one? Then please just: 

 - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/array/array_product.js)

### Example 1
This code
{% codeblock lang:js example %}
array_product([ 2, 4, 6, 8 ]);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
384
{% endcodeblock %}


### Other PHP functions in the array extension
{% render_partial _includes/custom/array.html %}
