6

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_command(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_command(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.

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. Commented Feb 21, 2019 at 22:16
  • 5
    if (Msys.inShip) send_command('ship launch'); 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. Commented Feb 21, 2019 at 22:22
  • Do you need the x afterwards? Commented Feb 21, 2019 at 22:29

3 Answers 3

5

I 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_command(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");

Sign up to request clarification or add additional context in comments.

1 Comment

This is what I was trying to find, thanks! I know there are easier methods to write out what I had above but I'm just exploring different ways javascript can be moved while I familiarize myself with the language. Thanks a lot!
0

An important distinction between your two approaches - The second approach will ALWAYS call send_command() whereas your first approach will conditionally call it.

This distinction will matter depending on your implementation of send_command, 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_command 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.

Comments

0

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 complication 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_command method
  // otherwise it should be the window alert
  var operation = inShip ? send_command : window.alert;

  function send_command ( command ) {
    console.log( command );
  }
  
  // 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 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.