AppleScript reads like English until you try to write it, and then it reads like a riddle. If JavaScript comes more naturally to you, macOS has had another option since OS X 10.10: JavaScript for Automation, or JXA. The same osascript command runs it.
osascript -l JavaScript -e 'Application("Mail").inbox.messages.whose({subject: {_contains: "invoice"}}).subject()'
-l picks the scripting language (normally a plain text script is treated as AppleScript) and -e passes one line of script. This one asks Mail for the subjects of every message in the inbox with “invoice” in the subject. Application("Mail") gets you the app, properties and elements follow with dots, and whose() is the filter. Note the () at the end: without it you get a reference to the subjects, not the text itself.
For anything longer, save the code in a file such as invoices.js and run osascript -l JavaScript invoices.js. To see what an app lets you do, open its scripting dictionary in Script Editor and switch the language to JavaScript; the dictionary shows the JavaScript names.
Fair warning about the documentation. Apple’s main reference for JXA is still the release notes from OS X 10.10, so you learn it from examples more than from a polished manual. And the first time Terminal controls another app like Mail, macOS asks you for permission, so a script that runs unattended needs that answered once by hand. Quoting is the other trap: wrap the script in single quotes for the shell and use double quotes inside it, like above.
Further reading: Apple: JavaScript for Automation release notes, osascript man page (SS64)