/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Operators == and !=

Can somebody explain why this works:

[CODE]
if (tableRow.style.backgroundColor == “#ffffff”)
{
// do nothing, I don’t really need this
}
else
{
// do something
}
[/CODE]

and this does not:

[CODE]
if (tableRow.style.backgroundColor != “#ffffff”);
{
// do something
}
[/CODE]

?

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@KorJul 23.2008 — 
  • 1. is your CSS attribute written locally in a style? Not in a class, in a style?

  • 2. even so, some browsers (Mozilla) will return RGB values [I]anyway[/I], not hexa


  • Tell us what you want to do, post the whole code, and we will find a solution.
    Copy linkTweet thisAlerts:
    @kurentauthorJul 23.2008 — That's not the problem. tableRow.style.backgroundColor has the value of #ffffff, it's just the usage of == or != that got me confused.

    Apparently "true" is not the same as "not not true"? ?

    I mean writting code when it is == is not the same as != and going into the else statement? :eek:
    Copy linkTweet thisAlerts:
    @KorJul 23.2008 — [I]That[/I] could be the problem. Once again, show us the code you have used. HTML included.
    Copy linkTweet thisAlerts:
    @kurentauthorJul 23.2008 — I tried it again and it works as expected. I did so many changes that maybe I had the #ffffff in uppercase at first and that caused the problem. :o

    But still, how could that be the problem. "true" is always the same as "not not true" (double negative).
    Copy linkTweet thisAlerts:
    @KorJul 23.2008 — 
    But still, how could that be the problem. "true" is always the same as "not not true" (double negative).[/QUOTE]

    No code, no answer. Always [B]true[/B] is the same with [B]not false[/B], but you are talking about Booleans, or you must first check whether the comparison returns a Boolean, and which Boolean.

    You must also note that == and != do compare [I]only[/I] the value of two terms, not the type. For a value&type comparison the operators are === and !== . Anyway, this could be another topic.
    Copy linkTweet thisAlerts:
    @kurentauthorJul 23.2008 — I don't have a code example hehe. Like I said, I must have done something else wrong for that not to work. It works as expected now, but you said that sometimes == is not the same as != and going into the else clause. Or maybe we misunderstood each other again?

    Doesn't using the == operator for comparison always return boolean? And I've never heard of different types of booleans?
    Copy linkTweet thisAlerts:
    @Declan1991Jul 23.2008 — == is not strict, so it converts the types to check. Compare<i>
    </i>if ("1" == 1) {
    alert("");
    }
    if ("1" === 1) {
    alert("");
    }

    Without seeing all the code that it was related to, it is impossible to know what was happening. With booleans<i>
    </i>if (true == true) {

    }
    if (true != false) {

    }
    always return the same, but we'd have to see what the other comparison was. You have to have had a code example, or how did you know it now works, and didn't before?
    Copy linkTweet thisAlerts:
    @KorJul 23.2008 — == returns a Boolean indeed, but I wanted to say that, before using a comparison as if() argument, make sure you know [I]which[/I] is the returned Boolean.

    Maybe we don't understand one each other indeed. You say "I don't have a code example". If so, how did you concluded in your first post that == works but != do not? Where did you used #ffffff values? In your mind? You must have tried some something in a document and verified within a browser, right?. Well, I wanted to see [I]that[/I] something. When talking about javascript you must post the code you have used and, most of the time, the HTML related code as well.

    More about Boolean logic in javascript:

    http://www.quirksmode.org/js/boolean.html
    Copy linkTweet thisAlerts:
    @kurentauthorJul 23.2008 — The rest of the code is irrelevant, long, quite complex and partly written in my native language. That coupled with the nightmare of copying and pasting code into the forum and getting all the spaces and tabs correct always makes me avoid posting too much code. ?


    I'll try to explain what the problem was:

    I was comparing to an uppercase "#FFFFFF" while the value in the variable tableRow.style.backgroundColor was a lower case #ffffff. This made me think that the code which used == operator was faulty while it could never work anyway. Out of desperation I changed to != and went into the else clause and I guess I must have changed the comparison into a lower case #ffffff also and so it started working which made me totally confused.


    Also I think we had a different idea of "boolean value".

    I was going for (something == somethingElse) gives you a boolean true or false.


    "[I]Everything can be explained, but too bad we don't understand the explanation."[/I] ?


    [B]EDIT:[/B]

    Doh now I now what went horribly wrong in my head. I was using the javascript upper case function and forgot the brackets!
    [CODE]
    if (row.style.backgroundColor.toUpperCase[B]()[/B] != "#FFFFFF")
    {
    // do something
    }
    [/CODE]
    Copy linkTweet thisAlerts:
    @KorJul 23.2008 — How can I make you understand? To debug javascript you must see at least:

    - the function

    - the event which trigger the function

    - the HTML element which handle that event (if the event is called from)

    - the HTML elements affected by the javascript code

    In some cases you need to know also:

    - the Doctype

    - if the code pass the W3 validator (if the HTML code is well formatted)

    - the CSS classes

    Now, if everything you keep posting is...
    <i>
    </i>if (row.style.backgroundColor.toUpperCase() != "#FFFFFF")
    {
    // do something
    }

    ...believe me, no coder in this world is able to guess what is all about. We have no magic bowl.

    Your case:

    - where is the function who nest that part of code?

    - what supposes to do that function / code?

    - which is the event to trigger the function and where is placed?

    - how is the HTML code of that row? Is there a local style or a CSS class?

    You say the whole code is complicated. OK, isolate the problem, build a small document - a table with a row and its CSS, put onclick or whichever event you want to trigger a simplified function, insert a small if/else block and see what happens. If the error persists, post that small test code here. Entirely
    Copy linkTweet thisAlerts:
    @kurentauthorJul 23.2008 — I don't think you are reading my posts entirely. :p

    I solved the problem. The solution was really easy. I wrote it up there, but you are either not reading or I suck at explaining things hehe. And the rest of the code is as irrelevant as is my shoe size.

    But don't get me wrong, I still appreciate you taking the time trying to help me, thanks!
    ×

    Success!

    Help @kurent spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.15,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...