toLowerCase()
方法返回转换为小写的字符串。
示例
const message = "JAVASCRIPT IS FUN";
// convert message to lowercase
const lowerMessage = message.toLowerCase();
console.log(lowerMessage);
// Output: javascript is fun
toLowerCase() 语法
toLowerCase()
方法的语法是:
str.toLowerCase()
其中,str 是一个字符串。
toLowerCase() 参数
toLowerCase()
方法不接受任何参数。
toLowerCase() 返回值
- 返回一个新字符串,表示调用该字符串的转换为小写的版本。
注意事项:
- 当对
null
或undefined
调用toLowerCase()
方法时,会引发TypeError
。 toLowerCase()
方法不会修改原始字符串。
示例:使用 toLowerCase() 方法
let str = "Hello World!";
let sentence = "Java is to JavaScript what Car is to Carpet.";
let lowercase_str = str.toLowerCase();
console.log(lowercase_str); // hello world!
let lowercase = sentence.toLowerCase();
console.log(lowercase); // java is to javascript what car is to carpet.
输出
hello world! java is to javascript what car is to carpet.
另请阅读