Sort of. But it's often one or the other, sometimes neither. You're still beholden to what the market wants (95% of the time that's another JS app). Even if you specialise you won't be always working on greenfield projects.
Even if 95% of the market want JS apps and you don't want to do JS, you only need a find a few clients that want what you're offering. Niching down is an advantage to how much you can charge and for finding ideal clients as well.
Maybe you're thinking more of being a general coding contractor who temporarily joins a coding team? I'm thinking more of someone who has involvement at the design stage or in a consultant role.
I think a key part is moving from the mindset of e.g. "I'm a JS programmer who can help you" to "I help X achieve result Y".
I can relate that's it's tough starting out though and it's tiring when people make it sound easier than it is. Good luck!
Look up guides on how to go freelance. You can target the projects you want and the technologies you want. Lots of people do this but you hear about it less regularly on HN.