Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.

Timeline for "elseif" syntax in JavaScript

Current License: CC BY-SA 4.0

16 events
when toggle format what by license comment
Oct 18, 2020 at 21:09 history edited mit CC BY-SA 4.0
Explaining why it works and what is really happening
Jun 20, 2019 at 20:57 comment added drooh wow 13 years in and just learned this... buried in phps elseif
Jan 8, 2019 at 19:39 comment added holdenweb In Python elif is valuable mostly because the else clause must be indented. Repeated use of else: if therefore pushes what is essentially sequential logic over towards the right.
Jul 12, 2017 at 15:42 comment added Meekohi It also helps to understand why this works. If you leave off the {}'s for an if/else statement in javascript, the parser applies this to the next statement only. So: else if {...} is the same as else { if {...} }.
Feb 16, 2016 at 12:08 comment added Reversed Engineer @HenrikErlandsson Adding the space (or at least not deleting the space) makes me happy. else if is alive and well in JavaScript, and is identical to elseif in other languages. Extra brackets are not required, but whatever blows your hair back.
Nov 2, 2015 at 15:10 comment added Henrik Erlandsson Mark displays inexperience with other languages and language design by raising his eyebrows at a question that would be normal to ask for people experienced in many languages. It's fine for Hari to ask this even if, as with many other questions, he could have googled it first. :) In many languages, elseif serves (and is implemented) as a compact switch/case statement. JS lacks that, and substitutes an unwieldy switch statement that makes no-one happy.
Apr 20, 2015 at 21:49 comment added zwol Nobody would design a language where you had to write if X { ... } else { if Y { ... } else { if Z { ... } else { ... } } } because, seriously, yuck. (Well, maybe something from the INTERCAL family would go there.) Single-keyword elseif (however it's spelled) tends to go along with mandatory block delimiters on all control structures, but I hear Swift has that with a special exception for else if. To sum up, there's always some way to do a chain of else-ifs, and the way it's spelled is just one of those annoying little variations you have to memorize for each language.
Apr 20, 2015 at 18:43 comment added mpen @jolsen I specifically meant elseif as a single keyword. There are many uses for else if.
Apr 20, 2015 at 16:29 comment added jolsen @Mark To claim that there is never a reason to use "else if" is incorrect. Else if statements are akin to using "switch", however it will allow for more elaborate comparisons. Switch, of course, allows for very simple comparisons only. So yes, there's definitely good reasons for using else if.
Oct 8, 2014 at 5:39 comment added user764357 @Mark It more shows a misunderstanding of bracketing. Brackets aren't necessary for if/else blocks, but people are encouraged to use them, so I can understand why this looks incorrect to a new user.
Aug 7, 2012 at 5:16 comment added mpen @JayK: Haha.. PHP has elseif, perl has elsif (I think), and Python has elif.. that kind of annoyed me at first, but... I guess it's kind of cute. It does serve a slight purpose in Python and PHP though, because it wouldn't work with their colon syntax otherwise.
Aug 6, 2012 at 22:39 comment added Jay K @Mark, I agree... but it messes me up sometimes because I'm used to languages that have an elseif. I know it's identical, but I wonder what javascript's reason is for leaving it out. I am glad, however, that they didn't use elif, because that's just wrong :)
Oct 23, 2010 at 21:12 comment added mpen When people ask questions like this.. I think it shows a fundamental misunderstanding. With if and else there really is no need of elseif.
Oct 23, 2010 at 19:29 vote accept Hari Gillala
Oct 23, 2010 at 19:21 vote accept Hari Gillala
Oct 23, 2010 at 19:21
Oct 23, 2010 at 19:08 history answered Jeff CC BY-SA 2.5