Skip to content Skip to sidebar Skip to footer

Why Does != Work And Just = Doesn't?

Here's my code: if (document.getElementById('hiddenButton').style.visibility != 'visible') { document.getElementById('hiddenButton').style.visibility = 'visible'; } else

Solution 1:

Your condition is actually an assignment:

if (document.getElementById("hiddenButton").style.visibility = "hidden") {

You should be using ==:

if (document.getElementById("hiddenButton").style.visibility == "hidden") {

Solution 2:

The = is an assignment operation.

The != is an inequality operator.

The == is an equality operator.

I guess what you need is the == operator. So replace your code with:

if (document.getElementById("hiddenButton").style.visibility == "hidden") {

Solution 3:

JS Comparison operators

==      is equal to 
===     is exactly equal to (value and type)
!=      isnot equal

For example:

var x = 1;  //define and assigned and now x equal to 1
x = 3;        //now x equal to 3if( x == 4) {
    //you won't see this alert
    alert('Hello, x is 4 now');
} else {
    //you will see this alert
    alert('Hello, x hasn not been changed and it is still ' + x.toString());
}

Solution 4:

I think your problem is that you are confusing the assignment operator ( = ) with the equality operator ( == or ===). the assignment operator set the left hand side equal to whatever is on the right hand side, and the equality operator ( == or === ) actually tests for equality.

Solution 5:

It's because simple "=" is not for comparaison. Use "==" instead.

Post a Comment for "Why Does != Work And Just = Doesn't?"