JavaScript中toUpperCase()方法的使用
JavaScript中的toUpperCase()方法是一个将字符串中的所有小写字母转换为大写字母的函数,可以用来改变字符串的大小写,以达到我们想要的效果。
使用toUpperCase()方法的语法如下:
string.toUpperCase();
其中,string是要转换的字符串。
下面是一个使用toUpperCase()方法的示例:
var str = "hello world"; str = str.toUpperCase(); console.log(str);
上面的代码将字符串“hello world”转换为大写字母,最终输出结果为:
HELLO WORLD
toUpperCase()方法也可以用于数组,比如:
var arr = ["hello", "world"]; arr = arr.map(function(item) { return item.toUpperCase(); }); console.log(arr);
上面的代码将数组中的所有字符串转换为大写字母,最终输出结果为:
[HELLO, WORLD]
JavaScript中的toUpperCase()方法可以用来将字符串或数组中的所有小写字母转换为大写字母,从而达到我们想要的效果。