无法获得用JavaScript打印的值?

时间:2016-12-02 01:29:40

标签: javascript

我只是在Chrome控制台中尝试不同的操作。我不确定以下代码我做错了什么,但是我收到了这个错误:

  参数列表

之后的

Uncaught SyntaxError:missing)

...代码:

function printToConsole(val1, val2) {
console.log("The value of" +  val1 + "and" val2 + " is " + (val1 + val2 ));}

3 个答案:

答案 0 :(得分:1)

您缺少+符号,因此javascript不知道您需要连接更多字符串并抛出错误而不关闭函数调用(缺少')')。

function printToConsole(val1, val2) {
  console.log("The value of " + val1 + " and " +
    val2 + " is " + (val1 + val2)); //missing + after 'and' 
}

printToConsole(4, 5)

答案 1 :(得分:0)

function printToConsole(val1, val2) {
  console.log("The value of " +  val1 + " and " + val2 + " is " + (val1 + val2));
}

您在“and”字符串后缺少concat操作(+)。

问候。

答案 2 :(得分:0)

as mentioned above you are missing the + sign in string between "and" and val2.

To add further there are lot of syntax checking tools available. ESLint (http://eslint.org/) is one of them. It works with most of the text editors and will highlight these kind of syntax errors immediately.

Below is one more flavor to write above code

(function(val1, val2) {console.log("The value of " + val1 + "and " + val2 + " is " + (val1 + val2 ));})(1,2);