Conditional JavaScript for Experts

Good read for JavaScript hackers … :wink:

4 Likes

Objective-C (well really C) has this neat use of the ternary operator where the first part is used if the value is not null. So you can do things like (psuedocode):

button.setLabel(title ?: "Untitled"); // use title, but if it is null use "Untitled"

That said, I personally find overuse of ternaries hard to read and they often require changes down the road when a value becomes something non-binary. I think the english if/else is more clear to beginners.

2 Likes

Totally agree and the switch / case statements. That is why I like wording var over let (just semantically) and dislike the shorthand notation (arrow) for function. Makes things harder to read for beginners. I must say I like the OR notation || for defaults these days. Also, instead of keeping code in an if statement inside a function I find it easier to just return a flow (show it right to the door).

3 Likes

Yup! I think there's a way to write code that is more "self documenting" than others, especially to beginners with a potentially smaller vocabulary.

Frankly I'm upset at JavaScript for adding so many features which basically do the same thing (though have slight differences...)

Not that I can use let with Hype's javascript, but I agree yet wonder how much of my avoidance is bias/antiquated thinking. Intrinsically "let a equal 10" is a more english grammatical sentence. I have liked that python always had or because that's more natural (being a native english speaker myself), but have wound up always using || because of consistency with other languages and general fluency in my mind.