Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Build a Random Number Guessing Game

Jose Gutiérrez
Jose Gutiérrez
9,370 Points

Is there any difference in the order of elements inside the if statement, I mean, is the same if(a===b) and If (b===a)?

Have I to follow any rule in the order of the elements when i make a comparaison inside an If statement?

1 Answer

Nope. a === b is the same as b === a. :)

A good example:

Let's say a is 3 and b is 3, too.

Our code is just a === b, and when we run the program, JavaScript behind the scenes changes a to 3 (that's a's value) and b to 3, too (also b's value). That means JavaScript replaces a and b both with 3:

3 === 3

Also, if our code was b === a, then it will also change to the code above since a and b are the same.

It's the same if a and b are different :) Order doesn't matter if you are using ===, ==, !==, or !=. However, with > and <, it could cause a difference since 1 > 2 isn't the same as 2 > 1.

I hope this post helps you. :smile:

Good luck! ~Alex

No problem :)