Using ternary operators in a function with javascript

2024/2/27 9:11:09

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.


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){
// If a random number is even, pass "even". If not, pass "odd"
foo(Math.floor(Math.random() * 10) % 2 === 0 ? "even" : "odd");

Related Q&A

Hardhat compile error Expected a value of type HttpNetworkConfig

Im attempting to follow the NFT tutorial here. I have set up the accounts on Alchemy and Metamask created the .sol file. I have a .env file in root that looks like this: API_URL = "https://eth-r…

Javascript string object readonly?

a=new String("Hello");a[0]==="H" //true a[0]="J" a[0]==="J" //false a[0]==="H" //trueDoes this mean I can only use Strings as arrays of chars by .split…

Catching a JSONP error on a cross-domain request

Im using jQuery.getJSON() on a URL (different domain) which may not exist. Is there a way for me to catch the error "Failed to load resource"? It seems that try/catch doesnt work because of …

How to compare two string values as integers?

I am converting and comparing two string values using if (parseInt(x)!=parseInt(y)) {The problem is if values are x="9" and y="09" the test returns false.How can I get fix this ?

Google spreadsheets: Assign a script to a button with parameters

I can successfully assign scripts to images in Google spreadsheets. My problem is with parameter passing.I have this script to write the current time on a cell.function Time(cell){var d = new Date();v…

How to import from React-Select CDN with React and Babel?

Ive created a react application following the steps on the React Website, but Im having issues utilizing the React-Select Library (particularly the Select Component).I can only use cdn files to load de…

Tracking purchase of particular product on different sites

I have a website on which other websites owner can list their products. For listing the product they need to manually create the product by providing title, description, image and link of products.When…

For Array, is it more efficient to use map() reduce() instead of forEach() in javascript?

1)As we know, theres no side-effect with map() and reduce(). Nowadays, we also have muti-core on cell phone. So is it more efficient to use them? 2)On the other hand, theres only 1 thread for js to ex…

Leaflet Markercluster - this._featureGroup.addEventParent is not a function

I am adding Leaflet.markercluster plugin to my existing Leaflet map.I am getting this error:Uncaught TypeError: this._featureGroup.addEventParent is not a functionIn relation to the line var locations …

Can you use javascript in a page action popup?

Im trying to build a chrome extension that uses the page action popup feature, but none of my javascript seems to be working. Here is the source:<!DOCTYPE html> <html><body><input …