Unicode JavaScript Variables and Functions

By  on  

We all get a kick out of unicode symbols, as we all saw in my Unicode CSS Classes post, and just as we can use them for CSS classes, we can use them for JavaScript variables and functions as well!  Let's say you want to shave a few bytes of post-minified JavaScript byes by setting false equal to a variable.  Why not do so by using a unicode variable?

var ಠ_ಠ = false;

if(someVar === ಠ_ಠ) { // If someVar is false...
	// ...
}

Hilarious.  The evil eyes signal a negative result.  What about saving a few byes on true?  The Beatles once said that "All You Need is Love", so let's use a heart:

var ❤ = true;

if(someVar === ❤) {
	// ...
}

You can do the same with function names.  Functions can be named after unicode symbols:

function ಠ(arg) {
	// ...
}

Of course all of this is a bit ... insane, and you should never use unicode symbols, but nonetheless it's possible.  Imagine starting a new job and seeing those symbols as meaningful variables -- you'd quit immediately!  Hilarious!

Recent Features

  • By
    5 HTML5 APIs You Didn’t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

  • By
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

Incredible Demos

  • By
    HTML5’s window.postMessage API

    One of the little known HTML5 APIs is the window.postMessage API.  window.postMessage allows for sending data messages between two windows/frames across domains.  Essentially window.postMessage acts as cross-domain AJAX without the server shims. Let's take a look at how window.postMessage works and how you...

  • By
    Multiple File Upload Input

    More often than not, I find myself wanting to upload more than one file at a time.  Having to use multiple "file" INPUT elements is annoying, slow, and inefficient.  And if I hate them, I can't imagine how annoyed my users would be.  Luckily Safari, Chrome...

Discussion

  1. I could see this improving minification when processing files with many variables/methods in the same scope (if they don’t use it already, I’m not sure).

  2. Unicode characters are useless for minification, each unicode character uses 2 bytes instead of 1 in utf8. Also, there is no reason not to use unicode in variable names if you properly declare encoding in your html.

    • Actually, some characters take up to 4 bytes, not just 2. The Kannada letter TTHA in the example rakes 3 bytes for example, so “false” was minified from 5 bytes to 7 :P

  3. lol, it’s a funny joke. but I think it may be realize if some protocol is developed in the future. so cool!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!