简体   繁体   中英

if else stament with the typeof check is returning undefined

updated

hey I tested your code...its breaking for another scenario if I give mobileVersion as sample.pdf its breaking...I am getting skyCloudmageProfilePic as pdfProfilePic...this is without remove typeOf jsfiddle.net/n9d08ko2

where as if I use your conditionI am getting skyCloudmageProfilePic as newDocProfilePic how to get as pdfProfilePic... providing your updated code in this fiddle jsfiddle.net/Lqjhyvmz

if (model.mobilePics) {
    skyCloudmageProfilePic = model.mobilePics;
 } else {
    skyCloudmageProfilePic = "newDocProfilePic";
 }
  • I am trying to remove typeof from my if condition.
  • if I remove typeof I am getting undefined for skyCloudmageProfilePic.
  • is there any way to fix it.
  • providing my code below.
  • the reson I am trying to remove typeOf is I need to fix the lint error.
  • since typeof is not accepted.
  • can you tell me how to fix it provding my code below

  • I was debugging in this fiddle when it reaches this line let kendotxtMenu = ""; you can see the value for skyCloudmageProfilePic as newDocProfilePic skyCloudmageProfilePic = newDocProfilePic

http://jsfiddle.net/fdnoz2ka/

  • but in this fiddle if I remove typeof, you can see the value for skyCloudmageProfilePic as undefined skyCloudmageProfilePic = undefined

http://jsfiddle.net/c4k6mqkg/

//if (typeof model.mobilePics != "undefined" && model.mobilePics != "") {
          //skyCloudmageProfilePic = model.mobilePics; // skyCloudmageProfilePic = newDocProfilePic
       // }

       //if (typeof skyCloudmageProfilePic == "undefined") {
         // skyCloudmageProfilePic = "newDocProfilePic"; // skyCloudmageProfilePic = newDocProfilePic
       // }

        if ( model.mobilePics != "undefined" && model.mobilePics != "") {
          skyCloudmageProfilePic = model.mobilePics; // skyCloudmageProfilePic = undefined
        }

        if (skyCloudmageProfilePic == "undefined") {
          skyCloudmageProfilePic = "newDocProfilePic"; // skyCloudmageProfilePic = undefined
        }

        let kendotxtMenu = "";
 if (model.mobilePics) {
    skyCloudmageProfilePic = model.mobilePics;
 } else {
    skyCloudmageProfilePic = "newDocProfilePic";
 }

This should work, in the first you check for undefined and an empty string, but a string will always evaluate to false if it is either undefined or empty.

After looking a little harder I realized your second check is pointless as well. I've adjusted it to fit better.

You should also use '===' wherever possible, this avoids equality checks doing funky things to you. Here's a little post that explains the triple check.

Which equals operator (== vs ===) should be used in JavaScript comparisons?

Use the Boolean value of the variable to check for defined/undefined state:

 var x = 1; var y = undefined; if (x) { // if defined console.log("I'm defined"); } if (!y) { // if undefined console.log("I'm undefined"); } 

Or, compare the variables with the type undefined instead of the string "undefined" :

 var x = 1; var y = undefined; if (x !== undefined) { // if defined console.log("I'm defined"); } if (y === undefined) { // if undefined console.log("I'm undefined"); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM