如果第一个字符串以特定的第二个字符串开头,则返回 TRUE JavaScript
javascriptweb developmentobject oriented programming更新于 2025/10/27 1:48:17
我们需要编写一个 JavaScript 函数,该函数接受两个字符串并检查第一个字符串是否以第二个字符串开头
例如 −
If the two strings are: “Disaster management report” “Disas” Then our function should return true
让我们来编写该函数的代码 −
示例
const first = 'The game is on';
const second = 'The';
const startsWith = (first, second) => {
const { length } = second;
const { length: l } = first;
const sub = first.substr(0, length);
return sub === second;
};
console.log(startsWith(first, second));
输出
控制台中的输出将是 −
true
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

