Why does != work and just = doesn't?

Clash Royale CLAN TAG#URR8PPP
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
document.getElementById("hiddenButton").style.visibility = "hidden";
This code shows and hide a HTML button when you click on another button.
But my question is why does that code work and this doesn't:
if (document.getElementById("hiddenButton").style.visibility = "hidden")
document.getElementById("hiddenButton").style.visibility = "visible";
else
document.getElementById("hiddenButton").style.visibility = "hidden";
=
===
because comparism is done with
== and not only with one =– Najzero
Feb 28 '13 at 15:36
==
=
== <-- That is the equality operator in javascript
– Pow-Ian
Feb 28 '13 at 15:36
Because = is an assignment not a comparison, you probably want == (or ===, I'm not a javascript programmer so not entirely sure which but it's one of those)
– jcoder
Feb 28 '13 at 15:36
@jbabey: Not really; OP is using assignment, not wondering why
('false'===false) == false.– Brad Christie
Feb 28 '13 at 15:46
('false'===false) == false
6 Answers
6
Your condition is actually an assignment:
if (document.getElementById("hiddenButton").style.visibility = "hidden") {
You should be using ==:
==
if (document.getElementById("hiddenButton").style.visibility == "hidden") {
Of course, how stupid of me. But when I do that it still doesn't work the first time I press the button, nothing happens. But when I press it the second time it works. When I use != I don't have that problem, any idea why?
– user2100788
Feb 28 '13 at 15:40
Also worth noting that the if() is satisfied as truthy as the assignment returns the assigned value which is what's being evaluated
– Alex K.
Feb 28 '13 at 15:42
@user2100788 Probably because the first time you click, the
style.visibility property is empty?– Ja͢ck
Feb 28 '13 at 15:43
style.visibility
Yep, if it is empty it does not equal to visible, it is undefined.
– Malharhak
Feb 28 '13 at 15:44
@Jack Yes that was the problem, if I use === and put "visible" under else instead of "hidden" it works.
– user2100788
Feb 28 '13 at 15:46
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") {
Seriously? I fail to see how sarcasm helps the user solve his problem. Just my opinion, but he probably wouldn't have asked the question if he wasn't serious ...
– Zak
Feb 28 '13 at 15:39
@Zak Agreed. Removed the sarcasm part. :)
– Praveen Kumar Purushothaman
Feb 28 '13 at 15:41
The "!=" is not the not operator, it's the inequality operator. Big difference :)
– Ja͢ck
Mar 1 '13 at 0:26
@Jack Updated. Thanks for notifying. :) Was too binary! :P
– Praveen Kumar Purushothaman
Mar 1 '13 at 5:28
JS Comparison operators
== is equal to
=== is exactly equal to (value and type)
!= is not equal
For example:
var x = 1; //define and assigned and now x equal to 1
x = 3; //now x equal to 3
if( 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());
for example's sake,
'1'==1 is true, but '1'===1 is false (number and string compared, both have same value but different types).– Brad Christie
Feb 28 '13 at 15:41
'1'==1
'1'===1
@BradChristie: precisely ! and thank you for removing single quotes.
– Kaf
Feb 28 '13 at 15:44
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.
It's because simple "=" is not for comparaison. Use "==" instead.
I arrived too late ;)
– JoDev
Feb 28 '13 at 15:38
Left = Right
Left = Right
This means, "Whatever the right side is, put it as the value for the left side."
All comparisons and other checks are done with two symbols to limit ambiguity and improper variable assignments when you simply meant to check a value.
!= means not equal to
== means equal
=== means equal and same object/datatype
= means "Assign the right side (Or what it evaluates to) to the variable on the left
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
=should be===.– Elliot Bonneville
Feb 28 '13 at 15:36