console.log作为if语句中的条件

时间:2014-09-30 10:30:51

标签: if-statement console.log

为什么底层计算的if语句不是真的?

是否可能无法将console.log作为if语句中的条件与数字一起使用?

// This is what a function looks like:

var divideByThree = function (number) {
    var val = number / 3;
    console.log(val);
};

// On line 12, we call the function by name
// Here, it is called 'dividebythree'
// We tell the computer what the number input is (i.e. 6)
// The computer then runs the code inside the function!
divideByThree(6);

if (divideByThree(6) === 2)
{console.log("I'm right")}
else
{console.log("I'm stupid")}

这肯定有效

var divideByThree = 2;

if (divideByThree === 2)
{console.log("I'm right no.2")}
else
{console.log("I'm stupid no.2")}

1 个答案:

答案 0 :(得分:0)

你没有返回divideByThree的结果,所以它将返回undefined,它不等于2

EDIT 添加返回函数

var divideByThree = function (number) {
    var val = number / 3;
    console.log(val);
     return val;
};
相关问题