JavaScript doesn’t have associative arrays

Versione italiana

This post is about one common JavaScript misconception: associative arrays.

JavaScript supports associative array syntax, but it doesn’t really have associative arrays. They are useless, because you can use objects.

Let’s create a simple object:

var movie = {
    title : '2001: A space Odissey',
    director : 'Stanley Kubrick',
    year : 1968

Here, we define an object variable (movie) with 3 properties (title, director, year). Since properties are always public, we can read them:

alert('Title: ' + movie.title);

Also, JavaScript is very dynamic, so we can also modify or set new properties:

movie.genre = 'sf';

We can also use the classic associative array syntax to access object properties:

alert('Title: ' + movie['title']);

This may seem useless… and in this example, it is. We shouldn’t use that syntax when it is not necessary. But we can use it to access a property that we don’t know a priori:

var prop = 'title';
alert('Title: ' + movie[prop]);

This is particularly useful within for … in loops, which are the JavaScript equivalent of foreach loops:

for (var i in movie) {
    alert(i + ': ' + movie[i]);


MariaDB Mode for CodeMirror

I needed a SQL editor written in JavaScript for a tool I use to rapidly modify my databases. I found CodeMirror. It works great, and it’s used by a number of important projects. The code is released with a MIT license (I would prefer AGPL3, but it’s ok).

CodeMirror supports many languages, and language scripts are called Modes. I tried the MySQL mode but… well, to be honest, I didn’t like it. Maybe I am not lucky, but the first statements I wrote (DELIMITER and TRUNCATE) were not highlighted. Also, floating point numbers were not highlighted.

So, I wrote a new mode and put it on GitHub: MariaDB:

First, I re-worked the code. Now it’s in Strict Mode, and it uses types more strictly. Then, I fixed a couple of minor bugs in MySQL mode: ‘–‘ comments did not require a space, and backticks could be escaped in identifiers with a ‘\’.

Then, I added something:
* a lot of new keywords from MySQL and MariaDB;
* float numbers (including esponents)
* variables (all syntaxes);
* bin and hex (all syntaxes);
* all types of comments;
* ‘?’ (for prepared statements).

Limitations? Well, it does not support automatic indentation, and many other features that maybe should be supported. However, I just wrote this mode to improve a tool I use. I may improve it if there is interest around it, but I will not work on it if I’m not sure it makes sense.

If you use MariaDB Mode and you have some comments/requests, let me know. If you improve it, please do a Pull Request on GitHub repo.


JavaScript Is NOT Java!

Versione italiana

When I tell that JavaScript has nothing to do with Java, people say that I am exaggerating. And maybe they think I am fighting some strange kind of holy war. But I am just trying to highlight a real problem. If you use Lisp, and someone convinces you that you are using Java… well, your programs simply can’t be decent. For 2 reasons: you are not using the features of Lisp, and you can not use the features of Java.

Here are some facts that people can hardly question:

  • Java has classes and interfaces, JavaScript has not. This is because Java is Object Oriented and JavaScript is not (altought it has an object type).
  • JavaScript has a function type and lazy evaluation, Java has not. This is because JavaScript is a functional language and Java is not.
  • In JavaScript you can add properties to an object at runtime, or even change its parent; in Java you can’t. That’s because JavaScript is dynamic and Java is static.
  • In JavaScript, there is no reason to use the new operator. And usually, there is a good reason to avoid it. In Java you can’t even say “Hello World” without new.
  • In JavaScript you always should nest functions. In Java you can’t, and (if you know what Java is) you don’t want to do anything like that.
  • Java has private and protected properties/methods. In JavaScript you encapsulate data in another way.
  • Yes, in JavaScript you can emulate class inheritance. But you should use Prototypes. Don’t pretend you are using classes, because JavaScript doesn’t have classes.
  • a[“b”] is an array in Java, but is an object in JavaScript.

I know that this list is not an explanation of what JavaScript is. But I hope I made clear that JavaScript is NOT similar to Java.
Is JavaScript a subset of Java? No. A dynamic functional language can’t be a subset of a static object oriented language. Unless you think that Haskell is a subset of C++.
Are their syntaxes similar? No. JavaScript syntax is a dirty cheap, because it makes you beilive you are instantiating classes or accessing an element from an associative array, when you’re doing something totally different.

JavaScript is similar to Lisp, not Java. And programming in Java using Lisp is one of the most (hard + stupid + useless) things a developer could try to do. But it’s not your fault. It’s ECMA‘s fault. Ask them why they cheated you.