This question already has answers here:
How do you use the ? : (conditional) operator in JavaScript?
(20 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I vaguely remember that I've seen custom values for a boolean true/false sometimes, but I have no idea what it is called or how exactly it was written.
So I have this code:
preferenceHotel = document.getElementById('preference-hotel').checked;
Now instead of preferenceHotel returning true or false, I would like it to return other values (yes or no), without writing an if statement checking whether it is true or false.
I remember something like this:
preferenceHotel = document.getElementById('preference-hotel').checked:"yes"|"no";
Does anybody know what I mean and know the name of it so I can read the documentation? (+ write it for my use case).
Thanks in advance!
You can do it by using Ternary operators in JavaScript
let preferenceHotel = (document.getElementById('preference-hotel').checked) ? "yes" : "no";
Conditional Operators
How about this:
preferenceHotel = document.getElementById('preference-hotel').checked ? "yes" : "no";
try:
preferenceHotel = document.getElementById('preference-hotel').checked?"yes": "no";
Related
This question already has answers here:
multiple conditions for JavaScript .includes() method
(19 answers)
Closed last year.
This is my code below, basically i want the bot to check two things. The users message needs to contain "how" + either "doing" or "bread". It works perfectly when i use only "doing" but not when I add the "bread" condition.
I need a clean and simple solution for this and hope someone can help me, bc for me thats the most logical way in archiving what i need :D
if(msg.content.includes("how") && msg.content.includes("doing" || "bread") ){
if(msg.author != token){
msg.lineReply("I am good sir")
}
}
if(msg.content.includes("how") && (msg.content.includes("doing") || msg.content.includes("bread")) ){
if(msg.author != token){
msg.lineReply("I am good sir")
}
}
This question already has answers here:
Check a radio button with javascript
(7 answers)
Closed 5 years ago.
Quick question I'm unable to resolve... I have a radio button with values of yes or no.
Basically I'm doing this:
if (complete === '1') {
getElementById('test-radio').value('Yes')
}
But it's not setting this to yes, it's still defaulting as no...
Any advice on how I can make this work?
You can use either radioButton.checked=true;
What you are doing isn't javascript syntax.
if (complete === '1') {
getElementById('test-radio').checked=true;
}
This question already has answers here:
How can I convert a string to boolean in JavaScript?
(102 answers)
Closed 6 years ago.
I read out a data-attibute, then I want to convert the string "true" into a boolean. At the moment I have to do a comparison in the javascript, is there a better way to do it? I don't know how to use this solution
HTML
<div data-nav='{ "nav": "true"}'>
JS
var data = JSON.parse($nav.attr('data-nav').toString());
data.nav = (data.nav === "true") ? true : false;
Try
<div id='test' data-nav='true'>
var truefalse = $('#test').data('nav');
.data should be able to evaluate it as boolean
here is an example in JSFiddle
https://jsfiddle.net/lismore/hx3gLvgw/
This question already has answers here:
How do you use the ? : (conditional) operator in JavaScript?
(20 answers)
Closed 7 years ago.
Recently I came upon a weird line in a code(Second answer, couldn't find a way to link directly to the answer).
This is it:
reverse = !reverse ? 1 : -1;
Well, I guess specifically the use of boolean ? option : option;
This looks like legitimately nothing I have encountered so far. I cannot find it in google! Please explain what this is, what is the syntax and how to use it, because I really want to know what this is, since it looks like some advanced smart thing...
Is that a if-else statement? Thanks for reading this and answering! :D
It's a ternary (conditional) expression:
var result = condition ? "true result" : "false result";
Example:
// since 1 is less than 2 result = "It's less!"
var result = 1 < 2 ? "It's less!" : "It's more!";
The ? : is the ternary operator. It was invented to simplify stuff like this:
if (booleanExpression) {
var x = expression1;
} else {
var x = expression2;
}
So the above would become:
var x = booleanExpression ? expression1: expression2;
They are equivalent but using the ternary operator is a bit more concise. I've mostly seen this used for conditional assignment or just making an if-else clause more compact/require fewer keystrokes. In the example you gave, I would imagine it is used for conditionally reversing some collection based on an argument or flag provided to the module.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
javascript ? : notation
what does the "?" operator mean?
it means an inline if
condition ? true_statement : false_statement
e.g
if(condition){
alert("true");
}else{
alert("false");
}
is the same as condition ? alert("true"): alert("false");
It, along with : comprises the ternary operator and is a shortcut for returning one of two values (the second and third sub-expressions), based on the result of the condition (the first sub-expression).
Wikipedia gives a good description: http://en.wikipedia.org/wiki/%3F:#Javascript
It's used like this:
var result = (condition ? value_for_true : value_for_false);
Example:
var result = (1 > 0 ? "It is greater" : "It is less");
The above example stores "It is greater" in the variable result.
On its own, ? does nothing except cause a syntax error when used without :.
It's part of the ternary operator.
// This simple if
if (25 > 23) {
alert("yes");
} else {
alert("no");
}
// Is the same as
alert(25 > 23 ? "yes" : "no");
You probably mean the ?:, or ternary, operator. Since this has been covered multiple times before, I'll refer you to this thread for a full explanation.