This question already has answers here:
JavaScript shorthand ternary operator
(10 answers)
Closed 5 years ago.
Can this be done in javascript
python code:
value = value if value else "Oompa Loompa"
Update:
the duplicate solution proposed does not answer my question, i would like something similar to what python does in js if possible, i have supplied another example for python below:
value = value if value >= 5 else 0
you can do something like
value = value || "Oompa Loompa"
or
value = value ? value: "Oompa Loompa"
You're looking for the ternary operator:
value = value ? value : "Oompa Loompa"
Related
This question already has answers here:
Access Javascript nested objects safely
(14 answers)
Closed 10 months ago.
I saw a function that receives a string parameter then performs some operations with it; like this:
const val = this.searchParam && this.searchParam.trim().toLowerCase();
My question is, why don't assign directly the processed string? Like this:
const val = this.searchParam.trim().toLowerCase();
I tried this in JS Bin to see if there's a difference, and the result is the same.
What do they exactly use the && operator?
In code snippet below, the first log writes undefined, the second throws an error:
searchParam = undefined
console.log(searchParam && searchParam.trim().toLowerCase());
console.log(searchParam.trim().toLowerCase());
Therefore, the result is not the same
This question already has answers here:
How to write a ternary operator (aka if) expression without repeating yourself
(17 answers)
Do we have a simpler ternary operator in JavaScript? [duplicate]
(2 answers)
Closed 2 years ago.
I'm looking for a neat way to do this:
return func(arr) >= 0 ? func(arr) : arr.length;
Calling func(arr) does not modify anything, so the above works fine.
But I would still like to avoid executing func(arr) twice.
Is there a neat way to achieve that in JavaScript?
Try:
let retVal = func(arr);
return retVal >= 0 ? retVal : arr.length;
This question already has answers here:
Check variable equality against a list of values
(16 answers)
Closed 3 years ago.
Is there a concise or better way to write this condition without having to use so many ||.
if (myVar != 'A' && myVar != 'B' && myVar != 'C'){
...
}
You can also express it like an array,
if (!['A', 'B', 'C'].includes(myVar)) {
// if myVar is none of the values in the array
}
edit: added negation to grab the opposite case as your example
This question already has answers here:
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 6 years ago.
What does || [] mean in the code below? Why might this be needed?
getPair: function() {
return this.props.pair || [];
},
[] is an empty array. ||, in this context, is a guard operator.
The statement says to return this.props.pair, but if this.props.pair is falsy, an empty array will be returned instead.
This question already has answers here:
Variable assignment inside an 'if' condition in JavaScript
(6 answers)
Closed 6 years ago.
I don't know if it's possible duplicate. Testing my code, sometimes I was wrong with because I put = and not == or === in if conditions:
Consider this code :
var a = 0;
if(a = 1) console.log('true');
I don't know why this is not an error and why returns true the condition (a = 1)
I guess that what it does is assign 1 to the a variable, but why this evaluates to true and there's no error?
you're setting a to 1 and then checking the truthiness of the result. Non-zero numbers in JavaScript are true, so you get what you see.
Like in math, things are evaluated left-to-right, with parens going first.
As it was said, it does assign to your variable and will return true for all values other than 0.
A way to avoid these kind of mistakes is to change the test.
if( 3 == a)
Here if you ever write (3 = a) you would have an error.