【问题标题】:In Javascript console.log I am not getting the result [closed]在Javascript console.log中我没有得到结果[关闭]
【发布时间】:2017-03-31 11:36:37
【问题描述】:

如果 lastnameundefined,我希望 console.log 仅打印 firstname,而不是我得到 firstname +在控制台上未定义。我可以在代码中更改什么,因此如果其中一个未定义而未在控制台中打印“未定义”一词,并且两者都被定义为同时打印它们,则它只会给我名字或姓氏。这是我正在尝试的代码:

    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);
    };

【问题讨论】:

  • 当您应该使用 ===== 时,您正在使用 = 进行比较。此外,您将'undefined' 放在引号中。删除那些。 if (lastName == undefined) {
  • if-else 语句使用==
  • 也应该是Greeting(firstName,lastName);,而不是Greeting(firstName + " " + lastName);
  • @squint 好的,如果您确定您的 var 应该存在,那么您是对的。 ty :)
  • @Cracker0dks:是的,在某些极端情况下避免这些错误是有意义的,例如可能存在也可能不存在全局定义,尽管window.SomeGlobal 也适用于这种情况。在这种情况下,无论哪种方式都很好。 :)

标签: javascript undefined console.log


【解决方案1】:

条件应该是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>

【讨论】:

  • 虽然这确实纠正了错误,但我注意到逻辑可能是错误的,因为如果lastNameundefined,它会打印firstName,但实际上并不知道是否firstName 有一个有用的打印价值。但它遵循 OP 的做法,已经足够好了。
  • 确实如此。谢谢指出
【解决方案2】:

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

【讨论】:

  • 永远不要使用“==”,而是使用“==="
  • @IceBox 那是完全不同的讨论。
  • @IceBox:从来没有?这是最重要的。
  • @squint 最佳实践!!!
  • @IceBox:最佳实践是了解您可以使用的工具,并在适当的时候以何种方式使用它们。对有用的运算符说“从不”不是一个很好的做法。
【解决方案3】:

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

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 + "!!!");
            }
        }

【讨论】:

  • 糟糕!,已更正。
【解决方案4】:

您使用 = 而不是 ==(= 表示赋值, == 表示比较)

 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>

【讨论】:

    【解决方案5】:

    也使用 === 而不是 == 来比较类型。

    firstName === undefiend

    【讨论】:

      【解决方案6】:
      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();

      【讨论】:

      • 为什么投反对票?我说的不对吗?
      • 更仔细地检查您的代码。
      • 请告诉我出了什么问题,我检查了它,即使使用 lint
      • 您将lastNamefirstName字符串 "undefined" 进行比较。您需要删除这些引号。
      • 哦,对了,我想我总是用typeof firstname == 'undefined'
      猜你喜欢
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 2018-08-09
      • 2017-09-04
      • 2022-09-29
      • 2021-07-22
      相关资源
      最近更新 更多