simple javascript question [duplicate] - javascript

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.

Related

Filter String/Array for multiple conditions [duplicate]

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

Declare custom value for boolean true of false [duplicate]

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

Create a function that informs you if you have the 'dota' characters in the provided parameter [duplicate]

This question already has answers here:
Contains case insensitive
(14 answers)
Closed 1 year ago.
Example: Dota is better than league of legends (true).
Counter Strike is better than Fortnite (false)
I couldn't develop the line of reasoning, could you help me? Thanks.
The simplest way is this:
function findDota (string){
if (string.toLowerCase().search("dota") != -1){
return true;
} else{
return false;
}
}
string.search() returns the position (0 indexing) of the first occurence of the search parameter in the string. If the search parameter isn't in the string it returns -1.
A more complex answer could use regular expressions (regexs) which can be very powerful.
function findDota (string){
if (string.search(/dota/i) != -1){
return true;
} else{
return false;
}
}
here /dota/i searches for the string dota. The i means it is case insensetive.
useful links:
https://www.w3schools.com/jsref/jsref_search.asp
https://www.w3schools.com/js/js_regexp.asp
https://www.w3schools.com/js/js_string_methods.asp

|| (or) in the if statement in JavaScript isn't working [duplicate]

This question already has answers here:
Multiple comparisons in an if statement using the logical OR operator
(5 answers)
Closed 5 years ago.
When I use the or in the if statement, it pretends like there is no ||.
Example:
if (code !== "" || null) {
document.getElementById("person_name").innerHTML = "Welcome " + name + "!";
} else {
document.getElementById("person_name").innerHTML = "I'm sorry, but you didn't enter your name.";
}
I am using the window.prompt in JavaScript to alert anyone to put their first name in the box and it should output it. If someone just presses enter without putting anything in the box, it should say, "I am sorry, but you didn't enter your name." If someone exits out of the prompt box, it should say the same thing.
The != operator has higher precedence than || so your statement reads as "if (code is not equal to empty string) OR null" and null always evaluates to false.
What you rather want is:
if (code !== "" && code !== null) {...

Sorry to ask such a dumb one, but what is this? [duplicate]

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.

Categories

Resources