Ageism is definitely a thing, but I'd challenge your assumptions about 40. I turn 40 in April, and make more money, and work on more interesting problems with new tech than ever in my career.. most of the developers I know my age are well employed. Then again, I'm in Texas - it may be a much different story in SV/SF or in "big tech". That said, a 40 year old still using the same stack they used at 30 is probably in trouble. It's less an age thing than one's ability and willingness to learn and grow.
When was the last time you went looking for a new job?
A /lot/ of companies insist on "culture fit", which unfortunately does include a lot of sexism, ageism and racism. There's been plenty of good discussions about this on HN, and the fact that you've been lucky enough to avoid the problem, doesn't necessarily mean that the problem doesn't exist - or that people who are discriminated against are just stuck using ten year old stacks.
Honestly, a long time. For a long time I've done either remote work or relied on my personal network.
I also can cheat the system a bit - even though I'm 39 I look super young and wear the typical tshirt and hoodie you'd associate with someone much younger. (I look in the mirror and see a 23 year old, so I think that shapes my personality)
Additionally, I'm in Houston, where many developers are in healthcare or energy, and they do tend to use older stacks (usually .NET or ColdFusion)
That said, I think it's just a series of data points, and what you think a career should look like.