An exemplary electronic device includes a housing, a tray, and an ejection mechanism. A slot, a receiving groove, and a slit are defined in a sidewall of the housing. The receiving groove includes a main portion and a reduced portion. The tray is received in the housing and aligned with the slot. The ejection mechanism includes a push member, a first magnet, a shield member, and a second magnet. When ejecting the tray, the push member is moved towards the reduced portion and the shield member moves away from the first magnet and the second magnet to generate magnetic force to drive the pivoting member to rotate, the first end portion is urged by the magnetic force to move away from the sidewall of the housing, and the second end portion is driven to push the tray to move outwardly through the slot.