JS Shorthand

Steps to reproduce: 1]using if statement:
let compValue;
if(randomNum>=0&&randomNum<1/3){
compValue = 'Rock';
}
console.log(compValue)//Rock
let compValue;
if(randomNum>=0&&randomNum<1/3){
compValue = 'Rock';
}
console.log(compValue)//Rock
2]using shorthand
randomNum>=0&&randomNum<1/3 && compValue='Rock';
randomNum>=0&&randomNum<1/3 && compValue='Rock';
=>This statement gives me an error stating ';' expected.javascript let compValue: undefined. Can anyone pls explain me this
60 Replies
Chris Bolson
Chris Bolson4mo ago
randomNum>=0&&randomNum<1/3 && compValue='Rock'; This line as it is doesn't make any sense and will return a JS error but not the one that you are getting. Can you provide the complete code block/function?
MarkBoots
MarkBoots4mo ago
maybe you want this??
let compValue = randomNum >= 0 && randomNum < 1 / 3 ? 'Rock' : undefined;
let compValue = randomNum >= 0 && randomNum < 1 / 3 ? 'Rock' : undefined;
Vandana
VandanaOP4mo ago
This is the shorthand for IF-ELSE
if(randomNum>=0&&randomNum<1/3){
compMove = 'Rock';

}
else if(randomNum>=1/3 && randomNum<2/3){
compMove = 'Paper';
}
else{
compMove = 'Scissors';
}
if(randomNum>=0&&randomNum<1/3){
compMove = 'Rock';

}
else if(randomNum>=1/3 && randomNum<2/3){
compMove = 'Paper';
}
else{
compMove = 'Scissors';
}
GUARD OPERATOR
randomNum>=0&&randomNum<1/3 && compValue='Rock';
randomNum>=1/3&&randomNum<2/3 && compValue='Paper';
randomNum>=2/3&&randomNum<=1 && compValue='Scissors
';
randomNum>=0&&randomNum<1/3 && compValue='Rock';
randomNum>=1/3&&randomNum<2/3 && compValue='Paper';
randomNum>=2/3&&randomNum<=1 && compValue='Scissors
';
This can be written as a shorthand using only if statement isnt it?
Jochem
Jochem4mo ago
not in javascript rather, randomNum>=0&&randomNum<1/3 && compValue='Rock'; isn't valid javascript I know some other languages allow that, but JS isn't one of them you could write if (randomNum>=0&&randomNum<1/3) compValue='Rock'; if you want
MarkBoots
MarkBoots4mo ago
this would do it, but it will assign 'false' as the else
const compValue = (randValue >= 0 && randValue < 1 / 3) && 'rock'
const compValue = (randValue >= 0 && randValue < 1 / 3) && 'rock'
if you want a single line, this could also work
const compValue = ['Rock', 'Paper', 'Scissors'][Math.floor(Math.random() * 3)];
const compValue = ['Rock', 'Paper', 'Scissors'][Math.floor(Math.random() * 3)];
Vandana
VandanaOP4mo ago
Its called as the && operator in js
Jochem
Jochem4mo ago
yes, and you can't use it to chain assignments
Vandana
VandanaOP4mo ago
Meaning?
Jochem
Jochem4mo ago
you can't use && and then at the end assign something to a variable it's just not allowed in JavaScript there are other languages where that works, but not in JavaScript
Vandana
VandanaOP4mo ago
instead of assigning what else can be done apart from that?
Jochem
Jochem4mo ago
it's a logical AND operator, so you use it to check multiple conditions
Vandana
VandanaOP4mo ago
randomNum>=0&&randomNum<1/3 && console.log('hey');
randomNum>=0&&randomNum<1/3 && console.log('hey');
can we do this?
MarkBoots
MarkBoots4mo ago
you have to wrap it, then it will work. Like I showed here (here an article explaining it: https://dev.to/dailydevtips1/javascript-if-shorthand-without-the-else-13ch)
Vandana
VandanaOP4mo ago
could you pls explain this in detail.
MarkBoots
MarkBoots4mo ago
no not really. i don't like these kind of assignments, for me it doesn't read nice. The && operator is used to combine the condition check with a value ('rock' in this case). As i understand: const compValue = (randValue >= 0 && randValue < 1 / 3) && 'rock' If the condition on the left (randValue >= 0 && randValue < 1 / 3) is true, the entire expression evaluates to the value on the right ('rock'). If the condition is false, the entire expression evaluates to false.
Vandana
VandanaOP4mo ago
the third example represents the same as i have done in my example
No description
MarkBoots
MarkBoots4mo ago
yea, but your condition has already the and && operator in it, so to make it logically correct, you have to wrap it in ()
Vandana
VandanaOP4mo ago
(randomNum>=0&&randomNum<1/3 )&& compValue='Rock'
(randomNum>=0&&randomNum<1/3 )&& compValue='Rock'
MarkBoots
MarkBoots4mo ago
yes. But again, ask yourself if this is easy to understand/readable. (imo, don't do this)
Vandana
VandanaOP4mo ago
I am getting your point .But the term says Shorthand.
MarkBoots
MarkBoots4mo ago
this is also a shorthand
compValue = randValue < 1/3 ? 'Rock' : randValue < 2/3 ? 'Paper' : 'Scissors'
compValue = randValue < 1/3 ? 'Rock' : randValue < 2/3 ? 'Paper' : 'Scissors'
Vandana
VandanaOP4mo ago
it throws an errror actually.
No description
MarkBoots
MarkBoots4mo ago
Sorry, i missed that, you're not assigning let compValue = (randomNum>=0&&randomNum<1/3 ) && 'Rock'
Vandana
VandanaOP4mo ago
why we cant assign here
MarkBoots
MarkBoots4mo ago
because that syntax is for a conditional expression. not an assignment. It's just not how that works.
Vandana
VandanaOP4mo ago
Got it. what if the randValue is 0.2. it seems confusing whether to get into rock or paper isnit?
MarkBoots
MarkBoots4mo ago
no thats 0.2 < 1 / 3 (0.33333)
Vandana
VandanaOP4mo ago
0.2<2/3 as well
MarkBoots
MarkBoots4mo ago
the first condition that matches will assign if smaller than 1/3 ? => rock else if smaller than 2/3 => paper else => scissors
Vandana
VandanaOP4mo ago
Got it .Thanks for explaining in detail .
MarkBoots
MarkBoots4mo ago
no problem
Vandana
VandanaOP4mo ago
compMove= (randomNum>=0&&randomNum<1/3 )&& 'Rock';
console.log(compMove);
compMove= (randomNum>=0&&randomNum<1/3 )&& 'Rock';
console.log(compMove);
=>compMove is false,rather we want compMove to be Rock
MarkBoots
MarkBoots4mo ago
yes, then probably the randVal is not smaller than 1/3 as I've said
If the condition on the left (randValue >= 0 && randValue < 1 / 3) is true, the entire expression evaluates to the value on the right ('rock').
==> If the condition is false, the entire expression evaluates to false.
If the condition on the left (randValue >= 0 && randValue < 1 / 3) is true, the entire expression evaluates to the value on the right ('rock').
==> If the condition is false, the entire expression evaluates to false.
Vandana
VandanaOP4mo ago
in some case it doesnt work
Vandana
VandanaOP4mo ago
randVal is 0.5785133209921858
No description
missymae
missymae4mo ago
waste of time
caldane
caldane4mo ago
The problem is your logic doesn't branch so it is as if only the line compMove=(randomNum>=2/3) && 'Scissors' is run as the other lines are just trampled by this line. Why not just chain ternaries. compMove = (randomNum>=0 && randomNum < 1/3) ? 'Rock' : randomNum < 2/3 ? 'Paper' : 'Scissors';
Vandana
VandanaOP4mo ago
getting your point for ternaries(if else-if).Why lines are trampled rather this is && operator(multiple if statement) i have to get to know why its not working.Else guard operator doesnt have meaning in mine example.
caldane
caldane4mo ago
let randomNum = .57;
let compMove = null;
// The following line will set compMove to false or 'Rock'
compMove = (randomNum >= 0 && randomNum < 1/3) && 'Rock';
// The following line will set compMove to false or 'Paper'
compMove = (randomNum >= 1/3 && randomNum < 2/3) && 'Paper';
// The following line will set compMove to false or 'Scissors'
compMove = (randomNum >= 2/3) && 'Scissors'

console.log(compMove);
let randomNum = .57;
let compMove = null;
// The following line will set compMove to false or 'Rock'
compMove = (randomNum >= 0 && randomNum < 1/3) && 'Rock';
// The following line will set compMove to false or 'Paper'
compMove = (randomNum >= 1/3 && randomNum < 2/3) && 'Paper';
// The following line will set compMove to false or 'Scissors'
compMove = (randomNum >= 2/3) && 'Scissors'

console.log(compMove);
===============================
let randomNum = .57;
let compMove = null;
// The following line will set compMove to false or 'Rock'
compMove = (randomNum >= 0 && randomNum < 1/3) && 'Rock';
// The following line will set compMove to 'Paper' or the previous value of compMove (which would be 'Rock')
compMove = ((randomNum >= 1/3 && randomNum < 2/3) && 'Paper') || compMove;
// The following line will set compMove to 'Scissors' or the previous value of compMove (which would be 'Rock' or 'Paper')
compMove = ((randomNum >= 2/3) && 'Scissors') || compMove;

console.log(compMove);
let randomNum = .57;
let compMove = null;
// The following line will set compMove to false or 'Rock'
compMove = (randomNum >= 0 && randomNum < 1/3) && 'Rock';
// The following line will set compMove to 'Paper' or the previous value of compMove (which would be 'Rock')
compMove = ((randomNum >= 1/3 && randomNum < 2/3) && 'Paper') || compMove;
// The following line will set compMove to 'Scissors' or the previous value of compMove (which would be 'Rock' or 'Paper')
compMove = ((randomNum >= 2/3) && 'Scissors') || compMove;

console.log(compMove);
Maybe that will make a little more sense, I feel like you are close to understanding the issue but there is just a little switch before it comes together. If this doesn't clear it up at all I can try again. You have three assignments statements but only the last one will win. So you can change it so the last one is the most comprehensive or you can change it down to one assignment. Another option would be:
let randomNum = Math.random();
let compMove = null;

compMove = (randomNum < 1/3 && 'Rock') || (randomNum < 2/3 && 'Paper') || 'Scissors';
let randomNum = Math.random();
let compMove = null;

compMove = (randomNum < 1/3 && 'Rock') || (randomNum < 2/3 && 'Paper') || 'Scissors';
This is still a single assignment statement but it is only using and and or to get the value.
Vandana
VandanaOP4mo ago
let randomNum = .57;
let compMove = null;
// The following line will set compMove to false or 'Rock'
compMove = (randomNum >= 0 && randomNum < 1/3) && 'Rock';
// The following line will set compMove to false or 'Paper'
compMove = (randomNum >= 1/3 && randomNum < 2/3) && 'Paper';
// The following line will set compMove to false or 'Scissors'
compMove = (randomNum >= 2/3) && 'Scissors'

console.log(compMove);
let randomNum = .57;
let compMove = null;
// The following line will set compMove to false or 'Rock'
compMove = (randomNum >= 0 && randomNum < 1/3) && 'Rock';
// The following line will set compMove to false or 'Paper'
compMove = (randomNum >= 1/3 && randomNum < 2/3) && 'Paper';
// The following line will set compMove to false or 'Scissors'
compMove = (randomNum >= 2/3) && 'Scissors'

console.log(compMove);
here though randomNum is <1/3 why is it returning false? thats my first doubt.
missymae
missymae4mo ago
because you're assinging it 3 times... "// The following line will set compMove to false or 'Scissors'" Bro, learn basic coding flow before you start writing garbage.
caldane
caldane4mo ago
Because the syntax you are using is boolean && string.
The boolean is for shortcutting the rest of the logic, if the first part is false then stop executing logic on the and as no further values matter. If the value is true check to see if the next value is true. So that means if the first part evaluates to false the assignment will be false, it will not even attempt to look at the string. However if it is true then further evaluation need to be had. Non-empty string are true in js but the do not return the value true but rather the value of the string. Therefore those assignment statements must return your string literal or false This isn't helpful, many times I have written esoteric code to understand the code better. This could very well be this person path to understanding basic code flow.
missymae
missymae4mo ago
I'm not trying to be helpful, I'm trying to communicate or shame someone into realizing they are wasting other people's time forcing code to do things they do not understand.
caldane
caldane4mo ago
You do not need to read this you are wasting your own time, don't blame others for your decisions. This is a forum for help, if you have no intention in being helpful your input is not welcome.
missymae
missymae4mo ago
They don't want to hear that some code patterns are not used because of readability; they don't want to slow down to understand ternaries and chaining, they want to get a thing to do a thing the way they want.
caldane
caldane4mo ago
There is value in that, pushing a code to its limits has merit in learning. If this were production code, it would be inadvisable, if this is just for learning, and I suspct it is since it is Rock, Paper, Scissors, then this is perfectly fine.
missymae
missymae4mo ago
Pushing code to its limit lol yeah, you can encourage this mess all you like.
Vandana
VandanaOP4mo ago
wrt this,
(randomNum >= 0 && randomNum < 1/3)
(randomNum >= 0 && randomNum < 1/3)
evaluates to true because randomNum <1/3 so the right part should be continued i.e 'Rock'
missymae
missymae4mo ago
it is, and then you re-assign it false twice. it doesn't magically break out of the code somehow.
caldane
caldane4mo ago
More accurately the following statement when true will return 'Rock'
(randomNum >= 0 && randomNum < 1/3) && 'Rock'
(randomNum >= 0 && randomNum < 1/3) && 'Rock'
More simply put:
//The following will never evaluate the true part because the false will shortcut it
compMove = false && true
//The following will never evaluate the true part because the false will shortcut it
compMove = false && true
So if that is the case then the following will also never return 'Rock'
compMove = false && 'Rock'
compMove = false && 'Rock'
So in your code sample from above what would this do:
compMove = false && 'Rock'
compMove = true && 'Paper'
compMove = false && 'Scissors'
compMove = false && 'Rock'
compMove = true && 'Paper'
compMove = false && 'Scissors'
compMove will be set to false; compMove will be set to 'Paper'; compMove will be set to false; Therefore trampling 'Paper'
Vandana
VandanaOP4mo ago
got it . this gives the clear picture. Highly appreciate the patience,efforts,time to give such an example and make me understand the flow.A very big thank you for you great help.Thank you
caldane
caldane4mo ago
no problem, I have seen many people help out on this discord and I believe most people see this as a way to learn and teach. When you are further down your coding journey then it will be your time to teach, but for now just keep trying to learn 🙂
Vandana
VandanaOP4mo ago
To make it into a single line and avoid multiple assignment to compVal why cant we do
(randomNum>=0&&randomNum<1/3 )&& compVal ='Rock'
(randomNum>=0&&randomNum<1/3 )&& compVal ='Rock'
false &&& short circuits
false &&& short circuits
Jochem
Jochem4mo ago
simply because JavaScript does not allow anything other than a variable name and optionally the keywords let, const, or in a pinch var on the left side of the assignment operator also, if I'm being brutally honest, it's entirely unreadable and likely to cause bugs as are most of the workarounds. Write code that is readable first
caldane
caldane4mo ago
Assignment has to follow {{varname}} = {{value}} so you cannot do {{value}} {{operator}} {{varname}} = {{otherValue}} This is why I proposed:
const randomNum = Math.random();
const compMove = (randomNum < 1/3 && 'Rock') || (randomNum < 2/3 && 'Paper') || 'Scissors';
// {{varname}} = {{value}} or {{otherValue}} or {{thirdValue}};
const randomNum = Math.random();
const compMove = (randomNum < 1/3 && 'Rock') || (randomNum < 2/3 && 'Paper') || 'Scissors';
// {{varname}} = {{value}} or {{otherValue}} or {{thirdValue}};
ἔρως
ἔρως4mo ago
instead of doing that, why don't you use arrays? ['rock', 'paper', 'scissors'][Number.random() * 3] if you want to allow translations, you can do this:
let options = {
'rock': 'Rock',
'paper': 'Paper',
'scissors': 'Scissors',
};

let keys = options.keys();
let key = keys[Math.random() * keys.length];

let text = options[key];
let options = {
'rock': 'Rock',
'paper': 'Paper',
'scissors': 'Scissors',
};

let keys = options.keys();
let key = keys[Math.random() * keys.length];

let text = options[key];
if you want to translate to other languages, just change the value for each key (btw, don't forget to truncate the number)
missymae
missymae4mo ago
You were told hours ago that you can't assign a value that way, and handed multiple "single line" solutions written in actually useable ways. Like I said, you don't want to learn, or aren't able to due to gaps in basics. I would be shocked if they are able to work with objects.
ἔρως
ἔρως4mo ago
you can, if you wrap in parenthesis that then becomes a valid expression minified code does this a lot, and this is HORRIBLE!!!! DO NOT WRITE CODE LIKE THAT you're not a machine, you're a human write code you can read in 2 days, not code only god and v8 will understand
vagge_b
vagge_b4mo ago
Exactly. The more I read this discussion, the more I thought I was looking at some production-built ultra-compressed JS external script manual generation tutorial. The only thing I anticipated to see but didn't are those random letter variable and function names and maybe, just maybe, you should add them to this code, you know, just for good measure. Jokes aside one reason some people (myself included) may get confused by && (especially because they might have learnt it through experiential learning, not text-book memorization) is because in jsx inside {} if you add a condition which is true && <Comp/> it will render
ἔρως
ἔρως4mo ago
that's because the && operator resolves the expression by passing the right-side element as the result if you do 1 && 5 you get 5 instead of true but again, the code that op is trying to write is code that only minifiers should be writting you need to focus on making human readable code
Want results from more Discord servers?
Add your server