在Google Cloud Platform(GCP)控制台中,使用console.log()
跨越多行输出是一个常见需求,尤其是在调试复杂的对象或长字符串时。你可以通过几种方法来实现多行输出。
模板字符串是ES6引入的一种新的字符串字面量,使用反引号(`
)包围,可以包含多行文本和嵌入表达式。
console.log(`This is a multi-line
log message using
template literals.`);
console.log()
语句你可以使用多个console.log()
语句来输出多行内容。
console.log("This is the first line.");
console.log("This is the second line.");
console.log("This is the third line.");
你可以在字符串中使用换行符(\n
)来实现多行输出。
console.log("This is a multi-line\nlog message using\nnewline characters.");
join()
你可以将多行内容存储在一个数组中,然后使用join()
方法将其连接成一个字符串,并在每行之间插入换行符。
const lines = [
"This is the first line.",
"This is the second line.",
"This is the third line."
];
console.log(lines.join('\n'));
以下是一个综合示例,展示了上述所有方法:
// 使用模板字符串
console.log(`This is a multi-line
log message using
template literals.`);
// 使用多个console.log()语句
console.log("This is the first line.");
console.log("This is the second line.");
console.log("This is the third line.");
// 使用换行符
console.log("This is a multi-line\nlog message using\nnewline characters.");
// 使用数组和join()
const lines = [
"This is the first line.",
"This is the second line.",
"This is the third line."
];
console.log(lines.join('\n'));
领取专属 10元无门槛券
手把手带您无忧上云