深入了解 JavaScript 字符串的 endsWith() 方法

endWith() 方法用于检查一个字符串是否以传入的字符串参数的值结尾。

'JavaScript'.endsWith('Script') //true
'JavaScript'.endsWith('script') //false

你可以传入第二个参数,一个整数值,如果存在的话,endsWith() 将会将原始字符串视为指定字符数的长度:

'JavaScript'.endsWith('Script', 5) //false
'JavaScript'.endsWith('aS', 5) //true

该方法是在 ECMAScript 2015 中引入的。