Will JavaScript Function Without the ‘function’ Statement?

Contributing Editor

Are you fed up typing ‘function’ in JavaScript code every few minutes? Should it be shortened? JavaScript inventor Brendan Eich thinks so and he revealed the following options during his keynote speech at Web Directions @media in London last month.

The first alternative is the florin symbol:


ƒ myfunction() { … }

I quite like it. It stands out nicely in code and it looks like the f in function. Unfortunately, you won’t find it in all coding fonts and it’s tricky to type (in Windows, hold down Alt and type 0131 on your numeric keypad. Mac users should try the Special Characters palette).

Next, we have the lambda symbol:


λ myfunction() { … }

The JavaScript geeks will think that’s very cool! Unfortunately, it suffers the same problems as the florin and you can’t even type it in Windows.

Finally, Brendan offered the plain old hash:


# myfunction() { … }

The majority of the audience voted for this symbol. It’s certainly easier to type, although I’m not sure it’ll stand out significantly in JavaScript code.

Other options may be raised and you should note that ‘function’ can still be used if you prefer it … your existing code won’t break.

As well as reducing your typing effort, the bandwidth savings could be colossal. For example, the minified jQuery library would become 4.3% smaller. That may only be 3Kb, but multiply that against the millions of downloads it receives every day.

Do you like any of these function-replacing options? Please cast your vote on the SitePoint poll or leave your comments below…

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • ahallicks

    Nooooo!!! That’s just another standard that’s going to take years to be implemented everywhere and we won’t be able to use for donkey’s years because SOME browser’s JS engine won’t support it.

    And then you’ll have some vendors adding their own prefix to it, like CSS3. And it will go on and on in the same way. At least we’re at a bit of a standard now with JavaScript (mostly, and with the support of libraries). Is there a fallback that can be implemented for non-supportive engines?

    • http://www.optimalworks.net/ Craig Buckler

      Possibly, although all the browser vendors are on the ECMAScript committee. Unfortunately, you’d still need to wait for browsers like IE8 to die (2042 perhaps).

      There may be some way of creating a JavaScript shim, but I can’t think how. Over to John Resig and Douglas Crockford!…

  • Phil

    ahallicks took the words out of my mouth. The best thing that can come of this would be to include it in the next ECMAScript version and forget about it for 10 years!

  • Adam Hepton

    Why stop there? Why don’t we reassign ‘var’ to ‘_’, ‘for’ to ‘£’, ‘while’ to ‘€’ and, oh I don’t know, ‘new Date’ to ‘@’?

    Because it’s a really stupid idea, that’s why not.

  • LFA

    Why not the §-symbol? An easy “one stroke” symbol on the keybord. And I also like to think i has a semantic value, due to the fact that funktions in JavaScript looks like paragraphs in the code…

  • http://www.terrellharris.net/ funktifyknow

    I don’t think the # will do either. It is used in Coldfusion as a variable wrapper, so when you are writing CF code and JS together it could get pretty muddy. Let’s just stick to function, it works and I don’t think the extra 8 keystrokes are killing us…

  • http://www.aplossystems.co.uk AplosSystems

    I think it’s actually a rather good idea, though the problem still stands for what ahallicks said.

    If it were to be implemented, I would rather choose the hash (#) symbol.

  • bulletproofpoet

    Well said, Adam.
    I think that it would make code a lot less readable and maintainable, so why bother. I’d prefer to stick to the standard, reliable “function”.

  • Andy Mo

    try finding the # key on a mac keyboad, Jobs: you twit!

    • tim

      Um … shift-3?

    • W2ttsy

      shift 3? You know, the key next to 2, which when shifted, turns it to an @ used in emails?

      What Mac keyboard do you have that doesnt support that?

  • Brian

    Is it possible to just make the function declaration optional entirely? What other Javascript code would be confused with name() {}; format?

    • Zach

      name()
      {}

      Is that a function call followed by block, or your function declaration?

  • LanguageGumbo

    With all the languages and frameworks (PHP, Python, Perl, Lua, Javascript, JQuery, etc.) they all have their similarities and some take on the rules and build on each other and thankfully are becoming closer and closer. This would go against that in every way. If anything Javascript should take on other languages’ ways-of-doing-things or keep things the same as they are. Each language has a purpose or purposes; but hopefully there will be fewer differences between them in the years to come.

  • http://xslt2processor.sourceforge.net boen_robot

    This is all a bad idea, as already discussed.
    If bandwidth is the problem, then it’s about time ECMA created a binary form of ECMAScript (ala JAVA byte code), in the same fashion W3C created EXI as a binary form of XML (the fact it’s not yet implemented in popular libraries and browsers is just a detail…). Browsers that support the binary form will get it, and with HTTP content negotiation, uncapable browsers will receive the text equivalent.
    If readability or having to type a lot is the problem – I prefer that over having code that doesn’t work in many browsers (none currently, but it will become “many” as soon as someone implements it). Besides, the function keyword is extremely readable, and personally, I’m already used to it from PHP.

  • http://www.deathshadow.com deathshadow60

    [b]Oh noes, I might have to type some characters — AAAAHHH.[/b]

    Maybe it’s because I was weaned off Assembly & Basic by Wirth family languages like Modula and Pascal, but the LAST damned thing we need is to make a C syntax family language MORE cryptic. It’s another of the reasons I can’t stand jquery, prototype and their kin…

    Much less two of the three symbols chosen are HARDER to type for English language users since we’d end up going to the numeric keypad to enter them… To hell with that, that just interrupts normal flow of typing taking longer – that’s like making someone entering information of a form leave the keyboard to go to the mouse; or even to the arrow keys (not a fan of ‘selects’).

    Sorry, to me this sounds like the same garbage as people who will bloat out their code just so they don’t have to type document.getElementById with a function — Said people usually having 20k of javascript reliant on 100k libraries to do the job of 4k due to ineptitude or reliance on techniques that they claim makes it ‘easier’. (see 99% of the garbage written using jquery)

    They want easier they should write verbose code with verbose variable and function names, learn when to comment and when not to comment and consistent formatting, and give up on these fat bloated trash libraries that are flushing perfectly good website ideas right down the toilet.

    • Anonymous

      If you don’t understand JQuery (56k-Google CDN) good luck getting a job hippie!

  • pradeep

    i prefer ‘$’ , may be its my personal opinion. but its going to pose a challenge to existing js libraries such as jquery and prototype

    Regards
    s.pradeep kumar

  • halil

    A geek has had some free fun time again :) Use whatever you like (ok, this is unrealistic, I don’t think we gonna jettison the good ole “function” in the next eon or so), but have a nice editor. It could be NetBeans, phpDesigner, Zend Studio, vi, emacs, Notepad++ or something. Then have it auto-complete most of the code for you.

    I currently type “fn” (either in PHP or in JS) and when I hit the auto-complete shortcut, it becomes
    function |() {

    }
    with the caret positioned at the place of the bar (|). I use most of the popular constructs that way. Not only does it shorten the time, but it decreases the amount of syntax errors as well. Recommended way to do things.

    The best coder writes the least code (in order to accomplish the same thing, or most probably better).

  • Captain DDL

    I’d prefer an abbreviated version of function, such as funct, or func, versus something that is hard to type like the examples you provided.

  • http://www.mikehealy.com.au cranial-bore

    I can just imagine beginner JS tutorials: “To create a function type ƒ myfunction(){}; by that way that’s not a lower case f it’s an ƒ, hold alt and type 0131.”
    If your on a laptop without a separate number pad, tough luck.

    Nah, I think a bit of clarity is worthwhile. func is about twice as fast to type as function and is more descriptive than ƒ or #

  • Wolf_22

    Who’s bright idea was it to even take an idea like this seriously!?

  • http://fvsch.com Florent V.

    I don’t see the point of replacing “function” with something shorter. You would save a few characters but make code more cryptic; not a good trade-off.

    What would be interesting if it can be done in a reasonable manner: a shortcut for anonymous function declarations. That’s when you type function(){…} just so that the instructions between the curvy brackets are not executed right away. Typical example:
    element.onfocus = function(){
    // do stuff
    }
    One you have just one like this, your code is still readable. When you start having embedded anonymous functions three levels deep, not so much.

    Also there’s the module syntax, which is even more complicated (both to write and to understand for novice coders):
    (function(){
    // some stuff
    })();

    Having clean alternatives for those would be good. But “function myFunc(params){…}” is perfectly fine.

  • imajeff

    I prefer not a single character, and not to keep changing standard keywords. I don’t see how any Java stuff will stay useful if standards keep changing so that my browser can no longer run it just because it’s new. I don’t like having to upgrade my clients all the time, and my father doesn’t even want to upgrade from Windows 98 SE !
    That said, I don’t see why something shorter wasn’t chosen from the beginning like “func” or “fn”, unless it was important to be clear.

  • Anonymous

    It would be nice if it could be used for minification. It make code less readable. If everyone used Google CDN size wouldn’t be as important matter.

    Wouldn’t it be great if JQuery was actually available in the browsers themselves?

  • http://www.lunadesign.org awasson

    Was the keynote on April 1st?

    My question is why would you even bother with changing the function declaration? Seriously, is JavaScript so evolved and hardened that there’s nothing left to improve on it? …Or perhaps Brendan wanted to stir up some controversy so he threw this idea out to the public to get a reaction.

  • searchlackey

    No I do not believe that is possible.
    http://www.searchlackey.com

  • http://bitdepth.wordpress.com/ mmj

    > For example, the minified jQuery library would become 4.3% smaller.
    That’s the catch. Minified but not gzipped.
    As long as it’s gzipped, you won’t really save any space by replacing “function” with a single character.
    This will be useful only for code that’s minified but NOT gzipped. If it’s gzipped, the space penalty for storing the phrase “function” will be once-off only, not once for every occurrence of the word.

  • Akram Abbas

    Good information about javascript.

    I will keep using ‘function’ keyword, it is something meaningful. Those symbols are not standard/not known to others. But, I would be forced to select one of those symbols. So, I will go with ‘ƒ’, because it denotes a function and used in Mathematics.. like ƒ(x) = y+2

    Thank you for good information. You rock SitePoint.

    Regards,
    Akram Abbas
    Web Worm… :D

  • http://www.satya-weblog.com/ satya prakash

    I liked the florin symbol but how to type it everytime is the problem.
    The solution explained above (Alt+0131) is not working in my Laptop. May be it is due to Laptop does not have a separate num keypad.

  • http://r.je TomB

    Gah! Please in the interest of code clarity do not do this horrible horrible hack.

    I hate the dollar symbol being used as a function. It’s non-descriptive and makes for hard to read code.

  • Emil

    I think ‘fn’ would be more suitable for shortcut of ‘function’. There is something similar in jQuery when you use custom functions you type $.fn.etc

  • Kamal

    Ohnoes! Are you really saving 8 keystrokes with an ƒ symbol?
    English keyboard users need to type Alt+0131 on Win, Ctrl+Shift+u and 0192 on Linux and i am guessing something equally messed up on Mac, you end up saving just 2 keystrokes… and consider leaving the main typing area and going to NumPad, holding down control/alt/shift keys – no way, i don’t want such a solution that wasn’t ever needed. function keyword is just doing fine. kthxbye

    • http://www.optimalworks.net/ Craig Buckler

      Well, yes, although most editors allow you to define macros or shortcuts. Of course, you can always set those up to type ‘function’ too…

      • http://www.lunadesign.org awasson

        Further on that subject autocomplete will write ‘function’ in two keystrokes and a return in VS2008. I haven’t tried it in Dreamweaver or Expression studio but as you mentioned a macro can be easily created if autocomplete doesn’t do the job.

        This idea of a shortcut for ‘function’ really is a solution looking for an nonexistent problem.

  • Swivelgames

    Fed up?? Who was the newbie coder that complained in the first place? I’ve never once in my life heard someone complain something so absurd as typing “function”. You want macros, go get an IDE that supplies you with them, but don’t change the language.

    function myFunc () { … }

    That is common language to programmers. Don’t make the language more cryptic or hard to understand just because you’re lazy. If size is the problem, gzip it or add a binary format. But don’t change the language format. The abstract combination of keystrokes while coding would ruin the flow and just mess everything up. Not a good thing. Not to mention the newbies who complained about having to type out the word “function” would then complain about the language being difficult and hard to understand.

    JavaScript is fine how it is. Leave it.

  • anonymous

    Completely agree with mmj. We should push the Gzip standards that *already exists*, instead of making plaintext code less readable.

  • http://www.darknessfalls.org.uk Ged

    couldn’t agree more. Silly non-story, and the download on jquery would be an irrelevance if we used central repositories. Oh wait, most of us do.

    I’d consider this a spoof story but IT has a history of improving stuff to death. Please IT, put the JavaScript down and step away. For all our sakes.