As a more 'up-to-date' method based on @Ibu's (currently accepted and perfectly valid) answer, you can use the forEach
method in JavaScript, like so:
let select_elem = document.createElement('select');
langArray.forEach((element, index) => {
let option_elem = document.createElement('option');
// Add index to option_elem
option_elem.value = index;
// Add element HTML
option_elem.textContent = element;
// Append option_elem to select_elem
select_elem.appendChild(option_elem);
});
Using a forEach
method tracks its own index, meaning you don't need to have to manually track your index (using something like var i=0; [...] i++
) and is - I would argue - neater and more maintainable than a for
loop. After all, you don't need to create any variables outside of the loop, or manually increment those variables, and - consequently - there is less chance of conflicts if the index variable (say, var i
) is used elsewhere (e.g. if you have a loop within a loop).
It should be noted that this recommendation comes in a context - there will ultimately be contexts when a for
loop is a best choice, and when a forEach
loop will be a best choice. As I personally prioritise maintainability over speed, I would by default opt for a forEach
loop. But that's not to say that it is the correct or only correct answer. Always, always, ALWAYS consider your personal use requirements over the advice of anyone on the Internet.
I have also swapped innerHTML
for textContent
. Ultimately the content within the option is always going to be text (you can't add HTML to an option) so using textContent
is more descriptive to what you are ultimately going to be doing. And textContent
is more performant than innerHTML
as it does not parse the data being fed in. In fact, a benchmark shows that just changing innerHTML
to textContent
can improve performance by over x2.5.
As such this is likely to be more performant and more maintainable for those reading your code in the future. In this instance, unlike with a for
loop, I can see no benefit to not using textContent
over innerHTML
.
Performance
One thing to note is that for-in
loops are more performant than forEach
loops. Though this is somewhat trivial (especially given the context), and in my opinion you should always prioritise maintainability over mere speed... unless performance is a vital criteria for what you are making (like, for example, if you are iterating over 1000s of values regularly and are experiencing bottlenecks). As the old saying goes though, "premature optimisation is the root of all evil".
Ultimately, maintainers of JS will work to increase the performance of most-used constructs, and the performance hit will in most cases be negligible and will probably decrease over time as devices get more powerful, and parsers are ever-more optimised.
The performance of an application that cannot be readily maintained, however, can be very high indeed. And will only ever go up, as poor maintainability precedes more bugs being introduced over time. Shaving 0.002ms off a loop now is not going to have any noticeable impact going forwards. A future developer trying to add a <strong>
tag to an <option>
element because they read, innerHTML = ...
and assumed they could add HTML will have an impact going forwards.
Side note for older browsers
The forEach
method works in all evergreen browsers, but not in IE11 or below. For this, you will need to either polyfill or use a service like Babel to compile the code into a pre-ES6 format.
You may also need to change the let
keyword to var
for older browsers too. The let
keyword does work in IE11, though it has a number of issues. If you do not require support for older browsers though, using the keywords let
and const
are great habits to get into.
for(element in langArray)
creates a global and is wide open for a bug, better to scopeelement
to the block withconst element
. Top answer repeats this mistake. – Shien