Olivera Kovacevic Olivera Kovacevic - 1 year ago 72
Javascript Question

Return all values from array in lowercase using for loop instead of map

var sorted = words.map(function(value) {

return value.toLowerCase();

This code returns all values from words array in lowercase and sorts them, but I wanna do the same with a for loop but i can't.

I tried:

for (var i = 0; i < words.length; i++) {

sorted = [];


Answer Source

With arrays, the += operator does not do what you expect - it calls .toString on the array and concatenates them. Instead, you want to use the array push method:

var sorted = [];
for (var i = 0; i < words.length; i++) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download