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.
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
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
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?
the
let test
part is the left side, the ternary is the right side. Pretty much just left or right of the assignment operator =
ohh that makes sense, thanks
The error you are getting may be "SyntaxError: Invalid Assignment Left-Hand Side"
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 passedIf 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
oh, it still does funky stuff
yeah
that's 🐑 bhaaaaaaaaaaaaaaaaad
🤣
im sleepy
by the way, this is NOT a replacement for an
if