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

JavaScript

Pass a condition as an argument

I have this code:

$scope.checkIfReady = function(condition){ if(condition){ //do something }else{ //do something else } };

$scope.checkIfReady(word === "banana");

Say word is a global variable or something.

I can't seem to get this to work, is there anyway to do this? Or is there a work around?

Thanks!

what exactly dosent work?

I was trying ta pass a condition into the function! I wanted to pass word ==="banana", only evaluate it once I got to the IF-statement inside the function!

1 Answer

First one thing to clarify. You can't pass a conditional, but the result of a conditional. In other words word === "banana" will be evaluated first and the true/false will be passed to the function.

Assuming this is Angular 1.x, the issue is probably that word is outside of $scope. This also places it outside Angular's watch/update mechanisms.

Allright! Thanks!

It was really never an issue, i'm just trying to keep my code as DRY as possible, and then I thought of this and wondered if there was any way to do it!

Thanks again!