Honestly, don’t bother. I spent six weeks reporting JXA defects before it was released and mostly got ignored. It’s simply not fit for purpose.
(I wouldn’t waste time filing bug reports either: once it shipped they pretty much abandoned it, and two years after the PM got fired and the entire department disbanded.)
My advice: stick to AppleScript for desktop automation. Unlike JXA it has users and documentation to help you when stuck, and while it stinks as a language it’s the only (nominally) supported solution that speaks Apple events right. You can always mix-n-match with other languages by shelling out to `osascript` (for trivial tasks) or calling AppleScript handlers directly via the AppleScript-ObjC bridge. e.g. See: appscript [dot] sourceforge [dot] net/asoc.html
p.p.s. If you’re really curious how a JS-AE bridge should work, `npm install nodeautomation`. I wrote that in a fortnight just to show it can be done right. Unsupported, so tends to break when Apple messes with the OS, but I periodically patch it up. And so it goes.