Saturday, August 21, 2010

λ as a variable name in JavaScript

JavaScript is a functional language, or at least has very functional roots we could all probably agree.

Functional languages are based on lambda calculus and the CS literature often uses examples where a lambda expression is called simply, "lambda."

In JavaScript, one common use of lambdas is when you register a callback function with some asynchronous operation like an ajax request. Another is for operations on collections of objects like visiting every item in an array.

I wondered if one could use the lambda character, λ, as a variable name in javascript. It would be more compact (one character instead of five six) and (more importantly) would look cool. Turns out you can do this!

See it run here.

Notice that the character encoding is set to UTF-16. If you don't do this you'll get errors. Since the encoding is set that way, I don't even use html entities like λ to write λ :)

Note: I haven't tried this in any version of IE but it appears to work in Chrome, Safari and Firefox.

Now, how do you actually type λ into a text editor? That took some digging. Here's what I did on OSX:

  1. Open System Preferences -> Language and Text -> Input Sources
  2. Scroll down to "Unicode Hex Input" and make sure that checkbox is checked.
  3. Open the US Flag (or nationality of your locale :) icon in the upper right of the OSX menu bar and switch to "Unicode Hex Input" (the "U+" icon)
  4. Now whenever I want a λ I can just hold down the Option key while I type 03bb and blammo I get a λ. Which by the way is still five key presses but looks way cooler than "lambda" and takes up less space on the screen.

I have no idea what you have to do on linux or windows.

You're right, this isn't very practical. And setting your encoding from UTF-8 to UTF-16 like, doubles the bytes you have to transmit. But you ARE gzipping all of your http responses aren't you? AREN'T YOU?


  1. It works in IE >= 6 too. I've tried it some time ago.

    What I did to type it faster was to create a TextMate snippet triggered by CMD+\, which is the same shortcut used in DrRacket, and which also resembles the way lambda functions are written in Haskell.

    Anyway, I've never used it in an actual project. It was just for fun.

  2. Software application devices that are paid in Google Play Store are offered by TuTuApp Vip Apk Know More for free We'll be discussing Guide to Download Tutu Helper Android App, APK, iOS the procedure of downloading tutuapp vip apk Tutu Helper App on Android and also Tutu Helper VIP App iphone devices.

  3. Whether you just intend to set up promptly at a campsite or obtain an Click Here extremely rapid entering your Outdoor Grill 2017: Guides, Top Picks and Reviews lawn, the Coleman Road Trip is Outdoor Grills developed to configuration Best Outdoor Grills Reviews swiftly. If you have restricted space.

  4. Fantastic blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any suggestions? Many thanks! Tutu Helper

  5. This information is very useful for me. thanks for sharing
    TUTUApp Free

  6. This comment has been removed by the author.

  7. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. tutu vip