javascript questions

Hey, i have a couple questions about js, 1) what's the logic behind putting conditions in brackets in if statements and loops? e.g.
if (a === b) {
...
}
if (a === b) {
...
}
2) when using Number.parseInt, let's say to get the result 10 out of the string "10aaa", why is the radix recommended to be 10 specifically? 3) what are the benefits of using a ternary operator rather than an if statement? and the benefits of using an if statement over a ternary operator? I'd appreciate any tips. Thanks in advance.
10 Replies
Jochem
Jochem2mo ago
1. That's just how it is, really. 2. Because if you use a string that starts with 0x or OX, it'll switch to base-16, which you probably don't want 3. Conciseness, and personal preference. Most of the time though, ternary operators are used as the right side of an assignment, and if-statements are used for more complex situations or flow control
snxxwyy
snxxwyyOP2mo ago
Ah okay I see. Thank you. Where you say ‘right side’, I’ve seen errors saying ‘left side’ in etc. what does that typically mean?
Jochem
Jochem2mo ago
let test = condition ? "a" : "b";
let test = condition ? "a" : "b";
the let test part is the left side, the ternary is the right side. Pretty much just left or right of the assignment operator =
snxxwyy
snxxwyyOP2mo ago
ohh that makes sense, thanks
clevermissfox
clevermissfox2mo ago
The error you are getting may be "SyntaxError: Invalid Assignment Left-Hand Side"
ἔρως
ἔρως2mo ago
it's important to explain that some old implementations would interpret 0123456789 as octal, because it starts with 0 while 10 is the default radix, it wasn't always and some implementations did other funky stuff when the argument wasn't passed
Jochem
Jochem2mo ago
If radix is undefined or 0, it is assumed to be 10 except when the number begins with the code unit pairs 0x or 0X, in which case a radix of 16 is assumed.
so not quite default 10
ἔρως
ἔρως2mo ago
oh, it still does funky stuff
Jochem
Jochem2mo ago
yeah
ἔρως
ἔρως2mo ago
that's 🐑 bhaaaaaaaaaaaaaaaaad 🤣 im sleepy by the way, this is NOT a replacement for an if
Want results from more Discord servers?
Add your server