I am new to Javascript and sort of working through the weeds on these ternary operators. I have this small code segment:
const x = MSys.inShip ? 'ship launch' : '';
if (x != '') {send_mand(x);}
While this works efficiently enough I am curious as to if it can be rewritten inside of the function call. Something like the following:
send_mand(const x = MSys.inShip
? 'ship launch'
: MSys.alert("You aren't in your ship!);
This may not make sense with the current example but it's the best idea I had at the time. Basically, I like the shorthand of the ternary style for easy if/then conditionals but I don't like how it's tied to a variable which must then be called. I'm looking for a way to use that shorthand without having to tie to a variable.
Finally, the purpose of this is to see if you are in the ship and if you are, launch. If you aren't don't do anything at all or just send an alert message.
I am new to Javascript and sort of working through the weeds on these ternary operators. I have this small code segment:
const x = MSys.inShip ? 'ship launch' : '';
if (x != '') {send_mand(x);}
While this works efficiently enough I am curious as to if it can be rewritten inside of the function call. Something like the following:
send_mand(const x = MSys.inShip
? 'ship launch'
: MSys.alert("You aren't in your ship!);
This may not make sense with the current example but it's the best idea I had at the time. Basically, I like the shorthand of the ternary style for easy if/then conditionals but I don't like how it's tied to a variable which must then be called. I'm looking for a way to use that shorthand without having to tie to a variable.
Finally, the purpose of this is to see if you are in the ship and if you are, launch. If you aren't don't do anything at all or just send an alert message.
Share Improve this question asked Feb 21, 2019 at 22:16 Jason Jason 611 silver badge4 bronze badges 4- 2 If you do not want the method to be called if the x is blank, then no, putting the ternary in the method call parameters doesn't make sense. – Taplar Commented Feb 21, 2019 at 22:16
-
5
if (Msys.inShip) send_mand('ship launch');
– James Commented Feb 21, 2019 at 22:18 - Right on, I just wanted to see if it was possible because the if/then in javascript is mindnumbing with all the brackets. Thanks for the help. – Jason Commented Feb 21, 2019 at 22:22
-
Do you need the
x
afterwards? – Bergi Commented Feb 21, 2019 at 22:29
3 Answers
Reset to default 5I am curious as to if it can be rewritten inside of the function call.
Yes, it can. But, if you do it there, then there is no need for a variable. You would be passing the function's argument directly inline.
Having said that, you can't pass that MSys.alert()
statement as the "else" value because it will be executed in all cases. You'd have to pass a value there that the function can use as its input argument
send_mand(MSys.inShip ? 'ship launch' : 'some other string');
Here's an example:
function foo(x){
console.log(x);
}
// If a random number is even, pass "even". If not, pass "odd"
foo(Math.floor(Math.random() * 10) % 2 === 0 ? "even" : "odd");
An important distinction between your two approaches - The second approach will ALWAYS call send_mand() whereas your first approach will conditionally call it.
This distinction will matter depending on your implementation of send_mand, but it sounds like you want the behavior of the first approach.
Additionally, You can't declare variables using const in a function call. If you just pass in the ternary operator, you will end up calling send_mand with either your string, or undefined (the return of calling alert() ).
However, as an answer to your question, yes you can pass the ternary operator to a function like any other value. The ternary operator is an expression that will return a value.
Technically, you could keep a variable (such as operation
) below, which references which method you want to execute, depending upon some conditional. And then you could pass that variable method the variable string it should get.
So, as you can see, it can be done. But look at how much plication was added to the process, rather than just using a simple if else statement.
function test_logic ( inShip ) {
// if they are in their ship, the operation should be the send_mand method
// otherwise it should be the window alert
var operation = inShip ? send_mand : window.alert;
function send_mand ( mand ) {
console.log( mand );
}
// give the operation the desired string
operation( inShip ? 'ship launch' : "You aren't in your ship!" );
}
console.log( "Not in ship test" );
test_logic( false );
console.log( "In ship test" );
test_logic( true );