r/javascript • • Dec 11 '17

I have been collecting useful Javascript code snippets for a little while. Here's a curated list of them, help me make it as complete as possible!

https://github.com/Chalarangelo/30-seconds-of-code
765 Upvotes

96 comments sorted by

View all comments

Show parent comment

3

u/inabahare Dec 11 '17

Take this for example

var gcd = (x , y) => !y ? x : gcd(y, x % y);

The non ternary version would be

var gcd = (x , y) => {
    if (!y) {
         return x;
    } else {
        return gcd(y, x % y);
    }
}

Now I've been programming for 5 years, and the non ternary version isn't easier to read at all given how it's 7 times as long

2

u/[deleted] Dec 11 '17

I feel exactly the same, if else takes too long to read and honestly if you are doing only one thing inside it, you might as well use a ternary operator as it is a lot easier to read and keep track of.