I have known a few people who have children that have suffered from this.
One person it's because their son was premature and therefore their bowls had not formed properly. Essentially the issues that were not to do with him being premature could be the same for any child.
If a child gets constipation once, they experience discomfort or pain on going to the toilet. This means that they tend to put off going to the toilet for longer, simply making the problem worse as they will be very backed up by the time they do go and it'll probably hurt more than the first time. One thing you could do to help the psychological barrier of going to the toilet often is to give your toddler a treat when they do go potty.
On the physical side of things, there could be a few reasons:
Not enough fibre in their diet
If your child's diet is mainly good and they have plenty of fruit and vegetables and few sweets and junk food, then it is worth introducing a little more fibre. You can do this with cereals such as weetabix or a bit more brown bread.
Not drinking enough fluid
If your child has a totally balanced diet, perhaps the problem is that their stools are too dry and hard to pass. Try to make sure that your child drinks plenty of water with every meal. Cod liver oil could also be used as it literally acts as a softener and lubricant for the gut.
Irritable Bowel syndrome could also be a cause.
If you have tried all of the above, your child has plenty of fibre, seems to drink enough and has an all round balanced diet, you could try cutting out wheat. Many people are wheat intolerant without knowing it and actually many people believe that we should not eat any processed foods and this includes flour. The thinking behind it being that our bodies have not evolved yet to cope with digesting unnatural foods.
Can I give my child laxatives if they are constipated?
I would advise against using laxatives on children. Laxatives actually force muscle spasms and this can be very uncomfortable, I'd be worried that this will make your toddler more scared of going to the toilet. However, the doctor could give them a very mild version. If you do go down this route, continue to use the laxatives even after the issue has gone, you should never suddenly stop laxatives as this could result in a sudden blockage again. Gently lower the amount and be sure to then follow best practices with fibre and plenty of water thereafter.