Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Daniel Taylor
4,448 PointsAre comparison operators only used in conditional statements?
Are comparison operators only used in conditional statements? If not, could somebody please provide a simple example of how else they can be used. Thanks, Dan.
2 Answers
Jacob Mishkin
23,118 PointsDan the answer is yes. here is an example of using comparison operators outside of a conditional statement.
note check out Ann Tudor's codepen. Just so you know this is really advanced stuff. I hardly know what is going on here.
Object.getOwnPropertyNames(Math).map(function(p) {
window[p] = Math[p];
});
var rand = function(min, max, isInt) {
var max = ((max - 1) || 0) + 1,
min = min || 0,
gen = min + (max - min)*random();
return (isInt)?(~~gen):gen;
};
var randsign = function() {
return (random() < .5)?-1:1;
};
Here is the result:
http://codepen.io/thebabydino/pen/zxdXbq?editors=001
I hope this helps.
Daniel Taylor
4,448 PointsHi both, thanks for your answers. Dan.
Giorgi Kiknadze
Courses Plus Student 1,690 PointsGiorgi Kiknadze
Courses Plus Student 1,690 PointsHere are examples where you can use comparison operators: Most often people use it in conditional statements, but also you can use it to assign true or false on variable.