Genesis 38 is placed between Joseph's sale and his story in Egypt, and the contrast is intentional. Joseph will resist temptation in a foreign land; Judah, in the promised land, drifts away from his brothers, marries a Canaanite, and lives carelessly. His sons Er and Onan are wicked in the Lord's sight, and the Lord takes their lives. Judah then withholds his youngest son, Shelah, from Tamar, leaving her as a widow without a future, contrary to the custom intended to protect widows and preserve a family line.
Tamar's response is a deeply troubling act of deception, which Scripture records without approving. Through it, she exposes Judah's hypocrisy: he is ready to condemn her, yet he is guilty. When she produces his seal, cord and staff, Judah confesses, She is more righteous than I. This confession is a turning point. The man who sold his brother and deceived his father begins to change; by Genesis 44 he will offer his own life for Benjamin.
Tamar bears twins, and in a struggle at birth reminiscent of Jacob and Esau, Perez comes first. Perez becomes the ancestor of Boaz and King David (Ruth 4:18-22). In the New Testament, Matthew 1:3 includes Tamar in the genealogy of Jesus, along with Rahab, Ruth and Bathsheba. God did not choose this family because of human goodness. He worked through sinful people to bring the Savior who would redeem sinners.