Magnetism in javascript

I'm entering a science competition, and need to model magnetism in Unity. Is there a way of writing this in Javascript?

You can use joints. Try it!

What I would do..

Make 2 game objects, then put some kind of attraction force between them. Something like moving one object toward the other and vice versa, so they appear "attracted." Then make the amount of movement is inversely proportional to the distance (Vector3.Distance) between the two objects. Then, the closer they get, the faster they go!

I would suggest to also use Rigidbody.AddForce:

Attach a SphereCollider with a wider radius set as Trigger and a RigidBody to your GameObjects.

Use OnTriggerEnter to start Rigidbody.AddForce in the direction of the other object/collider.

Increase/decrease the added force depending on distance.

(When both GameObjects collide you could use a FixedJoint or parenting.)