9

Mind blow of the week: JavaScript has no "else if".

It's always two tokens. Not one. It's NOT like python's "elif".

It's ALWAYS chaining an additional and DISTINCT if statement in the else clause of the first. It is NOT creating multiple comparison paths in the same if statement as it would seem.

For example:

if(a) console.log(a);
else if(b) console.log(b);
else console.log(c);

Simply needs more proper indentation to show which "if" the "else" actually belongs to:

if(a) console.log(a);
else
if(b) console.log(b);
else console.log (c);

Comments
  • 6
    This seems to be the case for a lot of languages. C and Java come to mind
  • 5
    This is *way* more common than you might believe. Though I must admit I hardly miss it considering I'm not a huge fan of tons of if/else blocks anyway.
  • 2
    Its because "else: if <condition>:" would be an error. It needs the ":" to start a code block. If python didn't use elif then it would need to interpret "else if" as a symbol itself. They probably didn't want to special case the use of a space in a symbol.
  • 2
    iirc, neither does Python actually, elif is just syntactic sugar. If you do an AST dump via the ast module you'll see that elifs become nested else-ifs.

    Might be wrong about this but I had to build a DSL in python and I remember that I didn't have to do anything to handle elifs after doing if-else. Or maybe I just don't remember.
  • 2
    In all fairness, if you need an "elseif" clause, you fucked up anyways...

    In my entire website, there is not a *single* "elseif" clause in the code that I wrote...

    Heck, I kinda even forgot they were a thing honestly...
  • 0
    @RedPolygon what old version of C and Java does not support else if branching?
  • 2
    @PepeTheFrog they do, but if I understand correctly it doesn't get parsed as a single "else if" token
  • 1
    At least it's not a batch script where you need to open parenthesis between else and if
  • 4
    functionally, it's identical
Add Comment