toUpperCase()
方法返回转换为大写的字符串。
示例
const message = "javascript is fun";
// convert message to uppercase
const upperMessage = message.toUpperCase();
console.log(upperMessage);
// Output: JAVASCRIPT IS FUN
toUpperCase() 语法
toUpperCase()
方法的语法是:
str.toUpperCase()
其中,str 是一个字符串。
toUpperCase() 参数
toUpperCase()
方法不接受任何参数。
toUpperCase() 返回值
- 返回一个新字符串,表示调用该方法的字符串已转换为大写。
注意事项:
- 当在
null
或undefined
上调用toUpperCase()
方法时,会引发TypeError
。 toUpperCase()
方法不会更改原始字符串。
示例:使用 toUpperCase() 方法
let str = "Hello World!";
let sentence = "Java is to JavaScript what Car is to Carpet.";
let uppercase_str = str.toUpperCase();
console.log(uppercase_str); // HELLO WORLD!
let uppercase = sentence.toUpperCase();
console.log(uppercase); // JAVA IS TO JAVASCRIPT WHAT CAR IS TO CARPET.
输出
HELLO WORLD! JAVA IS TO JAVASCRIPT WHAT CAR IS TO CARPET.
另请阅读