避免 JavaScript 中意外的字符串连接?
javascriptweb developmentobject oriented programming更新于 2025/10/30 5:00:17
为了避免在连接字符串、多个字符串和数字时发生意外的字符串连接,请使用反引号。
我们有以下 −
const concatValue = 'John,David,Mike';
var friendNames= `${concatValue}`;
上述值与字符串和数字连接 −
var studentNameWithFriends=` ${concatValue}| 'Carol' | 24 ` ;
以下是连接的完整 JavaScript 代码 −
示例
const concatValue = 'John,David,Mike';
var friendNames= `${concatValue}`;
var studentNameWithFriends=` ${concatValue}| 'Carol' | 24 ` ;
console.log(friendNames);
console.log(studentNameWithFriends);
要运行上述程序,您需要使用以下命令 −
node fileName.js.
这里我的文件名是 demo37.js
输出
这将产生以下输出 −
PS C:\Users\Amit\JavaScript-code> node demo37.js John,David,Mike John,David,Mike| 'Carol' | 24
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

