Roland
Roland

Reputation: 9691

Another way of writing an if statement?

It may sound a bit stupid, but is there a shorter way of writing the following if statement in less words :

if(auxiliars.environment == "Development") {
    less.env = "development";
    less.watch();
}

Because I have that statement as part of a function :

set_environment: function(environment) {
    if(auxiliars.environment == "Development") {
        less.env = "development";
        less.watch();
    }
}

And I was wondering if I can somehow return that two lines of code :

less.env = "development";
less.watch();

The main reason I'm asking is because in PHP I'm doing something like the following :

return (!empty($len)) ? hash('sha512', str_pad($stream, (strlen($stream) + $len), substr(hash('sha512', $stream), $this->round(strlen($stream) / 3, 0), ($len - strlen($stream))), STR_PAD_BOTH)) : hash('sha512', substr($stream, $this->round(strlen($stream) / 3, 0), 16));

And I was wondering if I can do something similar in JavaScript.

Upvotes: 0

Views: 308

Answers (3)

Bergi
Bergi

Reputation: 664196

Not really, why would you want? Your if statement is clean and easily to understand.

Yet, you might try the ternary operator:

auxiliars.environment == "Development"
   ? ( less.env = "development", less.watch() )
   : void 0;

But using the comma operator doesn't make your code better.

Upvotes: 3

Ian Gregory
Ian Gregory

Reputation: 5820

Yes, you can use the ternary operator

Upvotes: 0

adhominem
adhominem

Reputation: 1144

Javascript has the ? : statement, if that's what you are asking.

So

var x = 5;
var y = (x < 10) ? "bar" : "foo";

will assign the string "bar" to y.

Upvotes: 0

Related Questions