Skip to main content

Coulomb's law force between charges in JavaScript

Updated 2 min readMarkdown

Coulomb's law gives the force between two point charges: F = k q₁q₂ / r², with k = 9 × 10⁹ N m²/C². Here it is as a few lines of code. (I build PractiSci, where Coulomb's law is a free virtual lab.)

Force between two charges

const k = 9e9;
const F = (q1, q2, r, K = 1) => (k * q1 * q2) / (K * r * r);
console.log(F(2e-6, 3e-6, 0.3).toFixed(2)); // 0.60 N, repulsive

Inverse square check

console.log(F(2e-6, 3e-6, 0.6).toFixed(2)); // 0.15 N, double r gives F/4

Find a charge from the force

Two equal charges repel with 0.9 N at 10 cm:

const q = Math.sqrt((0.9 * 0.1 ** 2) / k);
console.log(q); // 1e-6 C = 1 µC

In a medium

Divide by the dielectric constant K. For water (K ≈ 80) the force drops by a factor of 80.

console.log(F(2e-6, 3e-6, 0.3, 80)); // 0.0075 N

FAQ

Attractive or repulsive?

Like charges repel, unlike attract. Use magnitudes in the formula and decide direction from the signs.

Common unit mistake?

Convert µC to C and cm to m before using k.

Where can I try it?

The free Coulomb's law lab lets you change both charges and the distance. More are in the Class 12 physics practicals.

No comments yet. Be the first to comment.