# JavaScript:

**Working with complex numbers**

## How to:

JavaScript doesn’t have built-in complex number support, but you can roll your sleeves up and handle it with objects and math. Here’s a quick take.

```
class ComplexNumber {
constructor(real, imaginary) {
this.real = real;
this.imaginary = imaginary;
}
add(other) {
return new ComplexNumber(this.real + other.real, this.imaginary + other.imaginary);
}
// ...add more methods (subtract, multiply, divide) as needed
toString() {
return `${this.real} + ${this.imaginary}i`;
}
}
const a = new ComplexNumber(1, 2);
const b = new ComplexNumber(3, 4);
const result = a.add(b);
console.log(`Result: ${result}`); // Result: 4 + 6i
```

## Deep Dive

Complex numbers have been around since the 16th century, thanks to Italian mathematician Gerolamo Cardano. They became crucial in various fields, like engineering and physics. In modern programming, they’re key for simulations and algorithms needing multi-dimensionality.

Now, JavaScript isn’t loaded for complex numbers natively. But besides the DIY option, you could use math libraries like math.js or numeric.js. They pack the power for heavier complex number lifting, adding perks like more operations, magnitude calculation, and argument finding.

Underneath the hood, when you operate with complex numbers, it’s like managing two separate numbers tied at the hip. Addition and subtraction are straight plays—match the real with real, imaginary with imaginary. Multiplication and division get spicy with cross-term dances and need more care.

## See Also

- MDN Web Docs on JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
- Math.js, a math library including complex numbers: https://mathjs.org/docs/datatypes/complex_numbers.html
- Numeric.js, another library: http://numericjs.com/documentation.html
- A deeper dive on complex numbers (math focused): https://mathworld.wolfram.com/ComplexNumber.html