What the what! I know <if/else> stuff; now ternary operators can do that in one line?
Why did I learn <if/else> ? It better be because sometimes it's better to use one over the other.
Here's what I mean:
var x = 7;
var y = 4;
if (x > y) {
result = "good job";
}
else {
result = 20;
}
//Below is the above code written using the ternary operator
result = x > y ? "good job" : 20;
See what I mean?
If I find out there's something after this that's even simpler, I'm going to be upset with these dudes.
Fist pump! I'm getting these down easy!
Maybe that's why they started with <if/else>? I hope that they had a plan when they did this.
So, the good news is that I can read the code they have set up for me and see what it means, the bad news is that now when I read anything and see punctuation commonly used in .js (<>?/+===) for instance, I start reading in 'code mode' when it's not code, and I get confused for a sec. That will probably go away after a bit. I hope?
That's all for me folks. I got a lot done today! See Y'all friday!
--Martin
So, the good news is that I can read the code they have set up for me and see what it means, the bad news is that now when I read anything and see punctuation commonly used in .js (<>?/+===) for instance, I start reading in 'code mode' when it's not code, and I get confused for a sec. That will probably go away after a bit. I hope?
That's all for me folks. I got a lot done today! See Y'all friday!
--Martin
To ensure that you have proper coding, be sure to declare the result variable using var.
ReplyDeleteAlso, you should try to use single ticks for the string values instead of double:
var x = 7,
y = 4,
result;
result = (x > y) ? 'good job' : 20;
I always used a double ticks because there might be a word like(they're)in there and the tick in they're would end the string, right?
DeleteAlso, I don't need to say var twice? Just do a comma and hit enter? That seems like it'd be useful. :-)
DeleteYour post isn't quite right, the conditional operator (aka inline-if) is a *type* of ternary operator. It isn't called *the* ternary operator. Few (if any) languages have any ternary operators other than the conditional operator, so this is likely where the confusion originates.
ReplyDeleteSo, there's no such thing as THE ternary operator, there's just different types, and what I was calling the ternary operator is actually a conditional operator which is a TYPE of ternary operator?
Delete