40 years old? You’re still a youngster! You have a lot of working years ahead of you, and you will have success if you try hard and believe in yourself.
I’m 55 and trying to get back into application development after a 10 year hiatus. I have to say it is discouraging when most of the people who are competing for the jobs that I want are far younger than I am. But, as people have mentioned, I feel that I have maturity and experience on my side, and I feel that I will be a far more stable employee than younger people who start looking for their next job six months after they take a job.
So, don’t be discouraged–you can do it! I wish you the best!