I went up the management chain for a while (startup CTO, etc.), and it kind of burned. Switched back to dev for a larger (200 headcount) e-commerce.
My experience is valued very, very highly. I look at designs from younger developers and help them shape it a bit, and it's normally appreciated. I am willing to bet I am a better developer now at 38 than I was at 28. I expect to be better still at 48.
The "young people are smarter" thing is a myth. This is not a competitive sport built out of hours worked: it's about being able to write good code, and that takes experience and practice. Keep crafting, good luck.
There's clearly no obvious reason why someone in his 40s shouldn't be able to develop software just as well as someone in his 30s. Apart from a more or less healthy lifestyle, staying competitive in this line of work is more about mindset than about age. You stay young by exposing yourself to new ideas and challenges. I've met 30 year olds who act twice their age because they've settled into a cosy big corporate job that doesn't require anything but will get them safely to retirement. I've also met 50 year olds who're still avidly learning new stuff and who've managed to remain quite competitive in the software consulting business.
Another thing to keep in mind is: Level up. When athletes in competitive disciplines can't keep up anymore they often become trainers and mentors. For various reasons this makes sense in software development, too. Coaching / mentoring / teaching is a rare skill, as is competent management.
Then there's always the opportunity to create your own software product (I know, easier said than done but much more doable still than in many other disciplines) and sell value instead of your time.
Keeps me out of the rat race of which tech to learn next, or at least doing it at my own pace. This year I'll be applying Rust.
Video editing is a far, far higher competition field than programming.
Lots more people wanting to do it, lots less jobs, on average for lots less pay and much worse hours.