JavaScript Ternary Operator Example with Functions

JavaScript ternary operator example with functions

Heh, there are some pretty exciting uses of ternary syntax in your question; I like the last one the best...

x = (1 < 2) ? true : false;

The use of ternary here is totally unnecessary - you could simply write

x = (1 < 2);

Likewise, the condition element of a ternary statement is always evaluated as a Boolean value, and therefore you can express:

(IsChecked == true) ? removeItem($this) : addItem($this);

Simply as:

(IsChecked) ? removeItem($this) : addItem($this);

In fact, I would also remove the IsChecked temporary as well which leaves you with:

($this.hasClass("IsChecked")) ? removeItem($this) : addItem($this);

As for whether this is acceptable syntax, it sure is! It's a great way to reduce four lines of code into one without impacting readability. The only word of advice I would give you is to avoid nesting multiple ternary statements on the same line (that way lies madness!)

Using ternary operators in a function with javascript

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");

JavaScript recursive function using ternary operator

// If you would like to do this in one line then correct solution would be:    let rec = n => n == 1 ? n : n + rec(n - 1);// Now you dont need to add the return keyword before
// This works as return statement is added before ternary operator function rec(n) { return n == 1 ? n : n + rec(n - 1); }
// This works function rec(n) { if (n == 1) return 1; return n + rec(n - 1); }

Return statement inside JavaScript ternary operator expression

No its not possible. The operands of the ternary operator are expressions. And return is a statement. You should use if else. According to MDN

Parameters


condition
An expression whose value is used as a condition.

exprT, exprF
Expressions with values of any type.

Using ternary operator in JavaScript to invoke two functions

Yes, that's valid code. It will invoke either function1() or function2(), but not both - depending on the value of type.

Ternary Operator inside an Arrow Function

As others have pointed out

  userInput === 'rock' || 'paper' || 'scissors' ? return getUserChoice : console.log('please enter a valid entry');

Is not valid JavaScript. It's not even really a valid construct.

The ternary operators are for choosing A or B based on a condition.

 if (condition) {
return A;
} else {
return B;
}

Using the ternary operators that's

 return condition ? A : B;

but you're returning getUserChoice which is function OR you're returning nothing as console.log('msg') is not something to return.

It's not clear what you wanted to happen. It looks like you wanted to print an error if the user's choice is not one of rock, paper, or scissors

const validateUserChoice = userInput => {

// normalizes all inputs to lowercase
userInput = userInput.toLowerCase();

// checks whether the inputs are valid
const isValid = userInput === 'rock' ||
userInput === 'paper' ||
userInput === 'scissors';

if (!isValid) {
console.log('please enter a valid entry');
};

return isValid;
}

???

Note there are faster ways to check if userInput is one of many valid options. One might be

const validInputs = {
rock: true,
paper: true,
scissors: true,
};
const validateUserChoice = userInput => {

// normalizes all inputs to lowercase
userInput = userInput.toLowerCase();

// checks whether the inputs are valid
const isValid = !!validInputs[userInput];

if (!isValid) {
console.log('please enter a valid entry');
};

return isValid;
}

It's also probably not best practice to mix validating with error reporting.

const validInputs = {
rock: true,
paper: true,
scissors: true,
};
const validateUserChoice = userInput => {

// normalizes all inputs to lowercase
userInput = userInput.toLowerCase();

// checks whether the inputs are valid
return !!validInputs[userInput];
};

if (!validateUserInput(someInput)) {
console.log('please enter a valid entry');
};

note that the !! just makes something that is falsey to actually be false.

validInputs[userInput]

Will either be true or undefined. Often that's good enough but if you really want it to be true or false then !! does the conversion as in

const userInput = 'bananas'
const temp1 = validInputs[userInput]; // temp1 = undefined
const temp2 = !temp1; // temp2 = true
const temp3 = !temp2; // temp3 = false

Which is what !! is doing



Related Topics



Leave a reply



Submit