js字符串首字母大写的不同写法

2023-05-12,,

写法一:

let name = 'hello'
name.charAt(0).toUpperCase() + name.slice(1)

写法二:

let name = 'hello'
name.slice(0, 1).toUpperCase() + name.slice(1)

写法三:

let name = 'hello'
name.substring(0, 1).toUpperCase() + name.substring(1)

三种写法的原理都是一样的,提取首字母转为大写,和剩余的字符一起组成一个新的字符

js字符串首字母大写的不同写法的相关教程结束。

《js字符串首字母大写的不同写法.doc》

下载本文的Word格式文档,以方便收藏与打印。