The following should work:
Application('Finder').windows.close()
Alas, JXA is made of Lame and Fail † so just throws an error when you run it, so you'll have to use a loop to close each window one at a time.
You do need to be careful though when iterating over an array of object specifiers, as only by-ID specifiers are guaranteed to be stable. (Remember: object specifiers are first-class queries, not pointers, so behave very differently to OO-style references.) On this occasion, jackjr's finder.finderWindows().forEach(function(w) {w.close()})
will do the job, because finder.finderWindows()
returns an array of by-ID specifiers. However, if the array contains by-index specifiers then you must iterate those specifiers from last to first or else you'll get off-by-N errors.
† (TBH, for any non-trivial automation work, you're best sticking to AppleScript. The language itself may be crap, but it's the only currently supported option that actually speaks Apple events right.)