Have a velocity variable (positive is right, negative is left, amplitude is number of pixels to change each loop):
var xVelocity = 2;
And use it to determine what to add to the xPos each loop:
xPos += xVelocity;
Then set it based on xVelocity and xPos so that the image will "bounce":
if(xVelocity > 0){
if(xPos > 250){
xVelocity = -2;
}
} else {
if(xPos < 10){
xVelocity = 2;
}
}