Replacing a 40–50 gallon tank with the same kind of tank costs about $1,000–$2,500 installed in most of the US, yet owners in recent threads were quoted $3,600–$6,000 for the same job. The spread comes from a few line items, where the heater sits and who you call. Switching to tankless or a heat pump is a different, larger job.
For most homes the best value is a new tank on the fuel you already have, from about $850 installed. Electric homes with a basement should look hard at a heat pump tank, which runs for about a quarter of the energy cost. Gas tankless only wins with high demand, soft water and a long stay.