首页 > 解决方案 > chrome 无法运行 console.log() Uncaught SyntaxError: Invalid or unexpected token

问题描述

那是我的html代码


     <body>
        <script src="../js/myjscode.js"></script>
    </body>

这是 myjscode.js


     function firstFunction() {
            console.log("
        hello world ");
    }

    firstFunction();

如果我console.log("hello world ");输入myjscode.js

是完美的输出hello world

但如果我输入

function firstFunction() {
        console.log("
    hello world ");
}

firstFunction();

在 myjscode.js 上

它显示在 chrome 控制台上 Uncaught SyntaxError: Invalid or unexpected token

我做错了什么?

标签: javascriptconsole.log

解决方案


那是因为当您console.log()Uncaught SyntaxError: Invalid or unexpected token. 要解决这个问题,请使用反引号而不是引号:

function firstFunction() {
  console.log(`
  hello world`);
}

firstFunction();


推荐阅读