Why was the US successful at the Battle of Midway?

The breaking of Japanese Naval codes gave the Americans first hand knowledge of Japanese intentions as far as targets and times of attack which gave the US Navy time to counter Japanese tactics and strategy .