This elastic potential energy calculator makes it easy to determine the potential energy of a spring when stretched or compressed.
Imagine a simple helical spring. You can compress or stretch it (to some extent, of course). To do it, though, you need to perform some work - or, in other words, to provide it with some energy. This energy is then stored in the spring and released when it comes back to its equilibrium state (the initial shape and length). Remember that the elastic potential energy is always positive.
Dont forget that you cannot compress or stretch a spring to infinity and expect it to return to its original shape. After you reach its elasticity limit, it will get deformed permanently.