在Javascript console.log中我没有得到结果

时间:2017-03-31 11:36:38

标签: javascript undefined console.log

如果姓氏 未定义,我希望 console.log 仅打印名字,而是获得名字+在控制台上未定义。我可以在代码中更改什么,因此如果其中一个未定义而未打印单词' undefined'在控制台中,如果两者都被定义为打印它们。这是我正在尝试的代码:

    document.getElementById("runButton").onclick = function run() {
        var lastName = "arm";
        var firstName;
        function Greeting(firstName, lastName) {
            if (lastName = 'undefined') {
                console.log("Hello, " + firstName + "!");
            } else if (firstName = 'undefiend') {
                console.log("hello, " + lastName + "!!");
            } else {
                console.log("hello, " + firstName + lastName + "!!!");
            }
        }
        Greeting(firstName + " " + lastName);
    };

6 个答案:

答案 0 :(得分:4)

条件应lastName===undefined而不是lastName= 'undefined'。您可以使用==(等于)或===(严格等于)来检查运算符。 =是赋值运算符。

 document.getElementById("runButton").onclick = function run() {
    var lastName = "arm";
    var firstName;
    function Greeting(firstName, lastName) {
        if(typeof firstName !== 'undefined' &&  typeof lastName !== 'undefined'){
            console.log("hello, " + firstName + " " +  lastName + "!!!");
        } else if ( typeof lastName !== 'undefined') {
            console.log("Hello, " + lastName + "!");
        } else if (typeof firstName !== 'undefiend') {
            console.log("hello, " + firstName + "!!");
        } 
    }
    Greeting(firstName,lastName);
};
<button id="runButton">Click Me</button>

答案 1 :(得分:0)

检查你的条件,它有错字。还

function Greeting(firstName, lastName) {
            if (lastName == undefined) {
                console.log("Hello, " + firstName + "!");
            } else if (firstName == undefined) {
                console.log("hello, " + lastName + "!!");
            } else {
                console.log("hello, " + firstName + lastName + "!!!");
            }
        }

答案 2 :(得分:0)

不使用单个=,而是使用双==。目前,通过使用您正在比较的两个等号,将lastName设置为undefined。所以改变这个。同样未定义也不需要在撇号中。

答案 3 :(得分:0)

您正在使用=而不是==(=表示分配和==表示比较)

 document.getElementById("runButton").onclick = function run() {
        var lastName = 'arm';
        var firstName;
        function Greeting(firstName, lastName) {
            if (lastName === undefined) {
                console.log("Hello, " + firstName + "!");
            } else if (firstName === undefined) {
                console.log("hello, " + lastName + "!!");
            } else {
                console.log("hello, " + firstName + lastName + "!!!");
            }
        }
        Greeting(firstName , lastName);
    };
<button id="runButton">Click Me</button>

答案 4 :(得分:0)

使用===代替==来比较类型。

firstName === undefiend

答案 5 :(得分:-1)

document.getElementById("runButton").onclick = function run() {
    var lastName = "arm";
    var firstName;
    mygreeting = Greeting(firstName, lastName);
    console.log(mygreeting);
};
function Greeting(firstName, lastName) {
    if (typeof lastName === 'undefined') {
        return ("Hello, " + firstName + "!");
    } else if (typeof firstName === 'undefiend') {
        return ("hello, " + lastName + "!!");
    } else {
        return ("hello, " + firstName + lastName + "!!!");
    }
}

你希望你的函数返回一个结果,然后将函数的返回值分配给一个变量,然后将该变量用于某些东西,在console.log()中这样小心;