Move object from a to b

I know this sounds like a question which has been asked before. But i cant find any answers on the net, so know im asking!
How do i make my object move 2 on the z-axis if i press space?

void Update() {
if(Input.GetKeyDown(KeyCode.Space)) {
Vector3 myPos = transform.position;
transform.position = new Vector3(myPos.x, myPos.y, myPos.z + 2f);
}
}

Alternatively:

void Update() {
     if(Input.GetKeyDown(KeyCode.Space)) {
          transform.position += Vector3.forward * 2f;
     }
}