SyntaxError:意外的令牌

时间:2014-07-16 15:37:25

标签: javascript if-statement

出于某种原因,意外的令牌不断返回。有关如何解决它的任何想法?

var sleepCheck = function(numHours);

if (numHours>=8)
{
    return "You're getting plenty of sleep!  Maybe even too much!";
}

else
{
    return "Get some more shut eye!";
}

2 个答案:

答案 0 :(得分:3)

javascript中的函数有{}的正文分隔符:

var sleepCheck = function(numHours) { // <-- START HERE

    if (numHours>=8)
    {
        return "You're getting plenty of sleep!  Maybe even too much!";
    }
    else
    {
        return "Get some more shut eye!";
    }
}; // <-- END HERE

答案 1 :(得分:1)

你的语法错了。试试这个:

var sleepCheck = function(numHours)
{
    if (numHours>=8)
    {
        return "You're getting plenty of sleep!  Maybe even too much!";
    }
    else
    {
        return "Get some more shut eye!";
    }
};