Brendan Eich said "ECMAScript was always an unwanted trade name that sounds like a skin disease". I think the general sentiment is shared fairly broadly.
I'm not a lawyer, but I have a few trademarks myself, and I believe it's possible to apply for a trademark even if the language or term predates it. However, if there's a significant gap between the language's release and the trademark application, that could raise other questions. For example, if JavaScript was created in the '70s but Oracle applied for the trademark in the late '90s, when the language became popular, that could be a more complicated case. In this instance, though, there's only a one-year difference! It's quite common for businesses to file for trademarks after they see value in protecting a term.
By the way, when I filed my first trademark application, Sun Microsystems filed an opposition. What a coincidence! This was before Oracle acquired them.