为什么会出现意外的标识符我显然是菜鸟

时间:2015-11-06 00:20:49

标签: javascript

function myFavFood(food){
    var msg = "";
    if(food === "pizza")
    msg = "I love " + food;
    if (food === "hotpockets")
    msg = food + " woo";
    if (food !== "pizza" && food!== "hotpockets")
    msg = "I have bad taste in foods";
    return 'msg'

}
var pizza = myFavFood("pizza")
var hot pockets = myFavFood("hotpockets")
var not hot pizza = myFavFood();

console.log(pizza, hot pockets, anything)

3 个答案:

答案 0 :(得分:1)

您不能在变量之间使用空格,例如:

// INVALID variable name
var hot pockets = "something";

// VALID variable names
var hotPockets = "something";
var hot_pockets = "something";

您可以找到更多信息和示例here

答案 1 :(得分:0)

变量不能有空格......消除那些,你的代码应该有效。

答案 2 :(得分:0)

删除变量之间的空格,例如var hot pocketsvar hotPocketsvar not hot pizzavar notHotPizza,会出现意外标识符错误。

var pizza = myFavFood("pizza")
var hotPockets = myFavFood("hotpockets")
var notHotPizza = myFavFood();

console.log(pizza, hotPockets, notHotPizza);

另外,请移除return 'msg'return msg周围的单引号以返回消息,而不是字符串"msg"

return msg;