This question already has answers here:
Using '&&' as a shorthand if statement?
(5 answers)
Javascript usage of && operator instead of if condition
(7 answers)
"if" shorthand with double ampersand
(4 answers)
Closed 1 year ago.
Let me get to this point straight. So I found a shorthand on an Instagram post for something like this:
const bool = true
function myFunction (){
//smth
}
if(bool){
myFunction
}
I can't remember what it was, something with the && operator. Anyone?
sorry for being a bit off topic but I really need this.
Perhaps you're thinking of just using && as it short circuits
const bool = true;
const notBool = false;
function myFunction(){ console.log("hello, world!") };
bool && myFunction();
notBool && myFunction(); // nada!
Related
This question already has answers here:
Javascript conditional return statement (Shorthand if-else statement)
(2 answers)
Closed 1 year ago.
I am trying to learn how can i return in expressions, it might be impossible but any close solution can be helpfull
var something = ()=>{
(true) && (return true)
}
console.log(something())
and is a logical operator , it checks the logic and return the bool
value .you cannot use return statement within logical operator.In that
place (just to look cool) you can use ternary operator
for example:
var something = ()=>{
num=4;
return (num === 4) ? "Correct!": "Incorrect!";
}
console.log(something())
Just take an if statement along with the wanted return statement.
if (true) return true;
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:
Using ternary operator in JavaScript to invoke two functions
(2 answers)
Closed 5 years ago.
I want a shorthand that runs a function if true and runs a separate function if false. Something like:
(condition)? function1:function2;
condition? function1():function2();
function a() {
return "a";
}
function b() {
return "b";
}
(1 == 2) ? alert(a()) : alert(b());
This question already has answers here:
How to observe value changes in JS variables
(3 answers)
Closed 5 years ago.
function getPercCalculated(x){
return (x*9.3)/100;
}
var x = 10;
var perx = getPercCalculated(x);
Instead of this I would like to call the getPercCalculated using dot operator like
var perx = x.getPercCalculated()
Can someone help me..!!
Number.prototype.getPercCalculated= function(){
return (this*9.3)/100;
};
This will attach the getPercCalculated to every number in your code tough
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.