0

I have searched on StackOverflow but no questions matched my specific dilemma. I have a div with a class foo that is set via css to display: inline-block.

I would like foo to be visible when the variable temp is a number or a string, but not display when it is not a number or a string.

var temp;
        if (temp !== undefined || temp !== null) {
            temp = temp / 1000;
            temp = temp + 'K';
            $('.foo').css("display", "inline-block");
        }
        else {
            $('.foo').css("display", "none");
        }

When I run this code the "foo" does not display at all.

when I change the code to

var temp;
        if (temp != undefined || temp != null) {
            temp = temp / 1000;
            temp = temp + 'K';
            $('.foo').css("display", "inline-block");
        }
        if  (temp == undefined || temp == NaN) {
            $('.foo').css("display", "none");
        }

foo is still all set to display:"none"

I ran console.log on each if, and when temp is a value it does go through the first if and when it is NaN or null it does go through the second if. But why does ONLY the display: "none" take affect?

Thank you in advance. - VS

EDIT Changed code for first if in first code block

To clarif, temp is constantly being reset to a new value above this if using a .get function:

var temp =this.model.get('custitem_temp');

and this value is sucessfully being set multiple times. SOME of my console for reference:

temp is set using .get: 80000
temp inside FIRST IF: 80000
temp is set using .get: undefined
temp inside FIRST IF: undefined
temp is set using .get: undefined
temp inside FIRST IF: undefined
temp is set using .get: 80000
temp inside FIRST IF: 80000
temp is set using .get: undefined
temp inside FIRST IF: undefined
temp is set using .get: 80000
temp inside FIRST IF: 80000
temp is set using .get: undefined

...

and so on.

EDIT02:

Thank you for all your help. So from the suggestions, my code is now dividing undefined and values

         if (temp === undefined || temp === null) {
            console.log("temp (should be undefined): " + temp)
            console.log("inside IF");;  
            $('.foo').css("display", "none");
         }
         else {
            console.log("temp (should be a value): " + temp)
            console.log("inside ELSE: " + temp);    
            temp = temp / 1000;
            temp = temp + 'K';
            $('.foo').css("display", "inline-block");
            console.log("after ELSE: " + temp);
        }

and console reflects:

temp (should be a value): 80000
inside ELSE: 80000
after ELSE: 80K
temp (should be undefined): undefined
inside IF
temp (should be undefined): undefined
inside IF
temp (should be a value): 80000
inside ELSE: 80000
after ELSE: 80K
...

Nevertheless, I still get display: none; on foo

EDIT 03:

I added a console.log to output the display value:

            if (temp === undefined || temp === null) {
            console.log("temp (should be undefined): " + temp)
            console.log("inside IF");
            $('.foo').css("display", "none");
            console.log( "display value: " + $('.foo').css('display') );
         }
         else {
            console.log("temp (should be a value): " + temp)
            console.log("inside ELSE: " + temp);    
            temp = temp / 1000;
            temp = temp + 'K';
            $('.foo').css("display", "inline-block");
            console.log("after ELSE: " + temp);
            console.log( "display value: " + $('.foo').css('display') );
        }

My console log gives some interesting insight: Starts off like before, except saying the display value is undefined

temp (should be a value): 80000
inside ELSE: 80000
after ELSE: 80K
display value: undefined
temp (should be undefined): undefined
inside IF
display value: undefined
temp (should be undefined): undefined
inside IF

but then after about 9 loops it starts assigning a value to display:

temp (should be a value): 80000
inside ELSE: 80000
after ELSE: 80K
display value: undefined
temp (should be undefined): undefined
inside IF
display value: none
temp (should be undefined): undefined
inside IF
display value: none
temp (should be a value): 80000
inside ELSE: 80000
after ELSE: 80K
display value: block
temp (should be undefined): undefined
inside IF
display value: none

Any explanations?

vs-works
  • 13
  • 1
  • 2
  • 6
  • 2
    where you are assigning value to `temp`? – Guruprasad J Rao Dec 29 '15 at 03:41
  • 1
    Is this wrapped in a `.ready()` function? – Drew Kennedy Dec 29 '15 at 03:42
  • 1
    `var temp;` is setting the the value of `temp` as `null`. (unless you assign the value in the between). – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Dec 29 '15 at 03:43
  • I am assigned value to temp before the if conditions...this is an altered version of a propritary system using NetSuite. The .ready() is not needed, because this is being minified via gulp into a master js file and then being loaded. "temp" gets loaded with values multiple times on the page and this script is to evaluate the display of "foo" on the page. – vs-works Dec 29 '15 at 03:47
  • `display:none` because 2nd if statement is being satisfied. ie. `temp` is `NaN || undefined` – sudo_dudo Dec 29 '15 at 03:49
  • @Tareq Mahmood, it's not `null`, it's `undefined` https://stackoverflow.com/questions/5101948/javascript-checking-for-null-vs-undefined-and-difference-between-and – Andrew Brooke Dec 29 '15 at 03:50
  • 1
    @AndrewBrooke, Yap, I just meant to say that, it's equivalent to `null`, so `temp != undefined || temp != null` should be false always. – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Dec 29 '15 at 03:52
  • Say the if statement returned true, even though it never will. How would one expect `temp = temp / 1000;` to work if `temp` is a string? – Drew Kennedy Dec 29 '15 at 03:55

3 Answers3

2

Did you check your console? Your second if syntax is wrong. It should be like this

if (tempMileageWarranty == undefined || tempMileageWarranty == NaN)'

Copy paste mistakes aside...

.foo is getting set to display: none; because temp is undefined..

The code var temp; is going to make temp undefined, (passing your second if) because it is never assigned a value.

This might be some useful reading: JavaScript checking for null vs. undefined and difference between == and ===

Andrew Brooke
  • 11,633
  • 8
  • 32
  • 53
  • Sorry about the syntax, when copying and editing I fudged the second if. I edited it to be correct. I check the console. when "temp" has a value it goes through my first if and then when it is undefined it goes through the second if. But regradless of temp having a value, it still does not display. – vs-works Dec 29 '15 at 03:44
  • 1
    I'm not sure why you are confused then, it's getting set to `display: none;` because it's undefined.. – Andrew Brooke Dec 29 '15 at 03:46
  • But when it is defined it still setting it to display: none...this is evaluating every time temp is being set to a new value via .get method – vs-works Dec 29 '15 at 03:52
  • Thank you for your fiddle. I guess there is something else not working in my system, because my code is still not working. I am constantly only being sent to the first if...temp being undefined or not. refer to my console.log in OP. – vs-works Dec 29 '15 at 04:13
0
t = typeof(temp);   
if(t == "undefined")
{
    console.log(temp +": do not display");  
}
else if(t == 'number' || t == 'string') {
    console.log(temp +": It is "+t+".So display the foo");
}

Try this code. I checked it with 3 value of temp.80000,"Foo",undefined and the result I got in console is like this.

80000: It is number.So display the foo
foo: It is string.So display the foo
undefined: do not display

I hope this will help you.

Maddy
  • 105
  • 7
  • Thank you...so I implemented what you said and I am stlll getting the same resulte, all display:none: Some of my console log: ++ 80000: It is number. So display the foo / temp (should be a value): 80000 / inside ELSE: 80000 / after ELSE: 80K / display value: undefined / undefined: do not display / temp (should be undefined): undefined / inside IF / display value: undefined / undefined: do not display / ++ you can see the display css value is "undefined" That is the part i do not get. – vs-works Dec 29 '15 at 05:11
  • Are you creating div dynamically ? – Maddy Dec 29 '15 at 06:38
  • Ok. I asked that because display property of non-existing div is "undefined". If script does find any div, it will return its display prop as undefined. But if you say div is already present, so don't mind. – Maddy Dec 29 '15 at 07:02
0

Quick note. Wanted to know if you noticed the double semicolon here.

console.log("inside IF");;  

Also I'd just do a quick and simple check on the defined state if temp and the ensure that it is not null.

if (temp && temp !== null) {
         console.log("temp (should be a value): " + temp)
        console.log("inside ELSE: " + temp);    
        temp = temp / 1000;
        temp = temp + 'K';
        $('.foo').css("display", "inline-block");
        console.log("after ELSE: " + temp);
 } else {
        console.log("temp (should be undefined): " + temp)
        console.log("inside IF");
        $('.foo').css("display", "none");
    }
andre mcgruder
  • 1,058
  • 1
  • 9
  • 11
  • I noticed the double semicolon after i posted it. I corrected that and that did not fix anything. If you look at my last edit of the OP I am checking (and console logging) the display state of foo...it starts out as undefined and then after about 9 iterations it receives a state (block/none) – vs-works Dec 29 '15 at 06:54
  • instead of the jQuery css functions try the show() hide(). They will achieve the same affect but may be better suited to what you are trying to do. "show for inline-block and hide for none". – andre mcgruder Dec 29 '15 at 07:23