0

I want to know if these two things are the same. In my brain they are. I am overall just trying to understand the javascript syntax better. In my brain these are the same. Please let me know why I am crazy! Thanks,

is this first one

if (edited === 'true' || edited === '')

the same as this?

if (edited == 'true' && '')
5
  • 1
    No they are not. In one condition, you are using || and second && Commented Jan 10, 2014 at 21:07
  • 2
    The first one has a possibility of being true. The second one doesn't! Commented Jan 10, 2014 at 21:08
  • Think if (expression && expression). Where each expression evaluates to either true or false and && evaluates the second expression if the first one is true. Commented Jan 10, 2014 at 21:11
  • Please read about Logical Operators and Comparison Operators at MDN. Commented Jan 10, 2014 at 21:45
  • @user3174713 - Please choose an accepted answer by using the green checkbox next to the answer that helped the most. This will allow future coders a reference to the issue. Commented Jan 16, 2014 at 2:14

3 Answers 3

6

No. They are completely different.

In the first one, it checks to see if either edited === 'true' or edited === '' is true.

In the second one, it checks to see if both edited == 'true' and '' are true. '' converts to false, so the second one is like doing edited == 'true' && false or just simply false.

Sign up to request clarification or add additional context in comments.

Comments

4

These are called logical operators.

Here is what each one does:

&& - Tests if BOTH conditions are true

|| - Tests if EITHER conditions are true


For your code:

if (edited === 'true' || edited === '') you could actually do...

if (edited === 'true || '') Tests if edited === true OR false and if either is true, run it.

if (edited === 'true' && '') Tests if edited === true AND false

Clearly the last one will never work, as edited is considered the same as false, null, 0, etc.

1 Comment

@user3174713 - Please choose an accepted answer by using the green checkbox next to the answer that helped the most. This will allow future coders a reference to the issue.
1

No. Empty quotes always returns a "falsy" value, so your second test will either return true && false, or false && false.

1 Comment

I get it now. I have a long ways to go it seems!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.