Well, you can do it in Javascript easily enough. For example, look at:
dhtmlxMenu - Javascript menu with rich client side API and Ajax support I think that code is free to download.
But since you can also do it using CSS without requiring Javascript at all, then CSS is a better option, unless you have a particular reason to use Javascript. Examples abound, for example:
Pure CSS vertical Drop Down Menu