In his first letter, John declares that "God is light and in him is no darkness at all". Using this truth about God's perfect holiness and purity, John then goes on to demolish several erroneous teachings that were infiltrating the church. For example, the false teachers were denying that walking in darkness (i.e. living in an habitual, unconfessed and unrepentant state of sin) broke fellowship with God. But God is light (holy purity) and, as a consequence, he has no fellowship, no communion, no relationship with unmediated sin. So walking in darkness means no fellowship with God is possible. To say otherwise is to deny God's holy revelation of himself... either denying God's holiness itself or denying that God expects holiness of us.
Fortunately, John goes on to say that if we walk in the light as God is in the light, that is if we embrace God's holy self revelation, then we can truly have fellowship with God and one another. But how? Coming to the light means that we cannot deny our sin because God's holiness makes our unholiness abundantly apparent, so what is the remedy? The blood of Jesus Christ cleanses us from all sin. The death of Christ, his work on the cross, is the only morally justifiable way that God can have a relationship with us. Christ's righteousness, his own moral purity, is given to us as a covering when we receive him in faith and confess and repent of the sin that God's light reveals in us.
John tells us that if we confess our sins (agree with God that our sin is sin and that sin is bad) God is faithful and just to forgive our sins and to cleanse us from unrighteousness. He is faithful because he promised to forgive and he keeps his promise when he forgives. He is just because Christ has paid the penalty for our sin and so the moral debt is wiped out. But we are not only forgiven, we are being cleansed of our sin as well. Thanks be to God!