BjornRunow BjornRunow - 3 months ago 14
Javascript Question

Convert array into upper case

I have an array that I want to change to upper case but I can´t make it work. Please help me.

var array2 = ["melon","banana","apple","orange","lemon"];

array2.toUpperCase()


I have also tried the below but it doesn´t work neither.

array2.map(toUpperCase);


Thanks in advance!

Answer

you should use handler function in map:

var array2 = ["melon","banana","apple","orange","lemon"];
array2.map(function(x){ return x.toUpperCase() })

for more information about map

edit: yes you can do

toUpper = function(x){ 
  return x.toUpperCase();
};
array2.map(toUpper);
Comments