I'm wondering if there's a known, built-in/elegant way to find the first element of a JS array  matching a given condition. A C# equivalent would be List.Find.

So far I've been using a two-function combo like this:

// Returns the first element of an array that satisfies given predicate
Array.prototype.findFirst = function (predicateCallback) {
    if (typeof predicateCallback !== 'function') {
        return undefined;
    }

    for (var i = 0; i &lt; arr.length; i++) {
        if (i in this &amp;&amp; predicateCallback(this[i])) return this[i];
    }

    return undefined;
};

// Check if element is not undefined &amp;&amp; not null
isNotNullNorUndefined = function (o) {
    return (typeof (o) !== 'undefined' &amp;&amp; o !== null);
};


And then I can use:

var result = someArray.findFirst(isNotNullNorUndefined);


But since there are so many functional-style array methods in ECMAScript, perhaps there's something out there already like this? I imagine lots of people have to implement stuff like this all the time...


Question: What is the difference between a coefficient and a subscript in a chemical equation?
Answer: 
  Why can't both of them be written in the same way? Does it matter? Is there a difference?


Yes, there is a difference, and it does matter! The placement of subscripts and coefficients tells you how the substances are put together on the atomic level.

Let's look at the first equation:

$$
\ce{2Li + S -&gt; Li2S} 
$$

This one indicates that two moles of lithium combine with one mole of sulfur to form one mole of lithium sulfide. $\ce{Li2S}$ is called a formula unit or empirical formula, and tells you the smallest whole-number ratio of atoms that make up a "building block" of the compound. In this case, the smallest building block of a lithium sulfide crystal would contain two lithium ions for every one sulfide ion. Here's a picture from wikipedia to demonstrate:



From this, we can learn that:


  Coefficients tell us the number of moles, molecules, or formula units of a species


and


  Subscripts tell us how many atoms or ions there are within a compound


Now if we look at the second equation:

$$
\ce{2Al + 3Br2 -&gt; 2AlBr3}
$$

we can see that in this case, it take 2 moles of aluminum to combine with three moles of bromine to produce two moles of aluminum tribromide. If it were instead $\ce{Al2Br6}$, that would indicate a different formula unit - it would imply a different compound.

So to answer your question:


  Why is Lithium in the first reaction written with a small 2, while in the second one Aluminium is written with a big 2


This is because in the first equation, the product has two lithium ions for every one sulfur in the formula unit, while in the second, two moles of aluminum tribromide are produced for every two moles of aluminum, but the formula unit contains only one aluminum atom and three bromine atoms.

For your final question:


  How do I tell which one to use?


You would have to know what the formula of the product is. In these examples, you can predict it by assuming that they are ionic compounds and balancing charges. You would get the correct answer, even though this assumption is not true for the second equation (it is actually a covalent compound, and you would just get the right answer by luck).