console.log拼接
时间: 2023-09-12 22:09:47 浏览: 66
console.log可以用于拼接字符串,可以将多个字符串或变量连接在一起输出。我们可以使用加号(+)来连接字符串或使用模板字符串来实现拼接。以下是一些示例:
1. 使用加号(+)拼接字符串:
console.log("Hello " + "World"); // 输出:Hello World
console.log("The result is: " + (2 + 3)); // 输出:The result is: 5
2. 使用模板字符串拼接字符串:
console.log(`My name is ${name} and I am ${age} years old.`); // 输出:My name is John and I am 25 years old.
需要注意的是,如果要拼接的是变量,需要将变量包裹在 ${} 中。这样可以将变量的值插入到字符串中。
另外,console.log还可以输出表达式的结果,比如算术运算或比较运算的结果。例如:
console.log(2 + 3); // 输出:5
console.log(2 > 3); // 输出:false
总之,console.log可以用于拼接字符串,也可以输出表达式的结果。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [JS基础第一课](https://blog.csdn.net/qq_51478745/article/details/125760865)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [字符串拼接占位符和conlose对象Api](https://blog.csdn.net/weixin_46873254/article/details/119185903)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]