threejs-math is just the math stuff from three exported on their own. Includes all the primitives you're asking about (and more) with methods, and also a good set of utility functions under MathUtils. https://ros2jsguy.github.io/threejs-math Answer from Aeverous on reddit.com
🌐
Victorjs
victorjs.org
Victor.js - 2D Vectors for JavaScript
A JavaScript 2D vector maths library for Node.js and the browser.
🌐
GitHub
github.com › tronkko › js-vector
GitHub - tronkko/js-vector: Vector and matrix math on JavaScript
Js-vector provides mathematical tools for doing 2d and 3d graphics in JavaScript.
Starred by 14 users
Forked by 3 users
Languages   JavaScript 99.7% | HTML 0.3% | JavaScript 99.7% | HTML 0.3%
Discussions

[AskJS] Good quality geometry, vector, etc library?
threejs-math is just the math stuff from three exported on their own. Includes all the primitives you're asking about (and more) with methods, and also a good set of utility functions under MathUtils. https://ros2jsguy.github.io/threejs-math More on reddit.com
🌐 r/javascript
10
13
April 7, 2023
vector - Element-wise Operations In Javascript - Stack Overflow
I'm doing some physics simulations which of course involve vectors. This has become very difficult for me because to the best of my knowledge javascript doesn't support anything like this... #with... More on stackoverflow.com
🌐 stackoverflow.com
math - javascript formula for computing the vector normal - Stack Overflow
The following question was asked and answered: Given 2 points how do I draw a line at a right angle to the line formed by the two points? However, I am wanting to use it in a javascript function ... More on stackoverflow.com
🌐 stackoverflow.com
javascript - Maths.js for Vector properties - Stack Overflow
I have been instructed to make a Maths.js which will have Vector properties such as adding, multiplication, dot product and cross product. Calculating these is fine, but i am new to javascript an... More on stackoverflow.com
🌐 stackoverflow.com
May 24, 2017
🌐
GitHub
gist.github.com › jjgrainger › 808640fcb5764cf92c3cad960682c677
A simple Vector class in javascript · GitHub
// set the direction of the vector in degrees Vector.prototype.setDirectionD = function(directionD) { var angleD = directionD; var magnitude = this.getMagnitude(); this.x = Math.cos(angleD * 180 / Math.PI) * magnitude; this.y = Math.sin(angleD * 180 / Math.PI) * magnitude; };
🌐
Evanw
evanw.github.io › lightgl.js › docs › vector.html
vector.js
Provides a simple 3D vector class. Vector operations can be done using member functions, which return new vectors, or static functions, which reuse existing vectors to avoid generating garbage · The methods add(), subtract(), multiply(), and divide() can all take either a vector or a number ...
🌐
Radzion
radzion.com › blog › linear-algebra › vectors
Vectors and Vectors Operations With JavaScript - Radzion.com
May 3, 2019 - In real-life applications those methods will not be enough, for example, we may want to find an angle between two vectors, negate vector, or project one to another. Before we proceed with those methods, we need to write two functions to convert an angle from radians to degrees and back. ... const toDegrees = radians => (radians * 180) / Math.PI const toRadians = degrees => (degrees * Math.PI) / 180
🌐
Jcoglan
sylvester.jcoglan.com
Sylvester
Sylvester is a vector, matrix and geometry library for JavaScript, that runs in the browser and on the server side. It includes classes for modelling vectors and matrices in any number of dimensions, and for modelling infinite lines and planes in 3-dimensional space.
🌐
npm
npmjs.com › package › vector-math
vector-math - npm
$\mathbf{\bar{a}}=\mathbf{\bar{u}}\times\mathbf{\bar{v}}$ const u = new Vector(1, 2, -3); const v = new Vector(-4, 1); const a = u.Cross(v); // a = i + 12j + 9k // a.value = { i: 3, j: 12, k: 9 }
      » npm install vector-math
    
Published   Dec 01, 2023
Version   1.1.1
Author   Willie Potgieter
Find elsewhere
🌐
Medium
medium.com › discourse › how-i-wrote-a-simple-mathematical-library-for-vectors-in-javascript-173072ee14ad
How I Wrote a Simple Mathematical Library for Vectors in JavaScript | by Spike Burton | Dialogue & Discourse | Medium
July 20, 2019 - Utilizing this system, we can represent every point on the grid as an ordered pair in the form (x, y) in relation to the origin (0,0). Now, imagine drawing a line from the origin to the point — that is a vector.
🌐
Math.js
mathjs.org › docs › reference › functions › dot.html
math.js | an extensive math library for JavaScript and Node.js
Calculate the dot product of two vectors. The dot product of A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] is defined as: dot(A, B) = conj(a1) * b1 + conj(a2) * b2 + … + conj(an) * bn · math.dot(x, y) Type | Description —- | ———– · math.dot([2, 4, 1], [2, 2, 3]) // returns number 15 math.multiply([2, 4, 1], [2, 2, 3]) // returns number 15 ·
🌐
GitHub
gist.github.com › gordonbrander › 9569045
n-vector: generic, efficient JavaScript vector math using ordinary arrays or indexed objects · GitHub
September 16, 2018 - n-vector: generic, efficient JavaScript vector math using ordinary arrays or indexed objects - n-vector.js
Top answer
1 of 7
22

we can use the map function to add array elements:

function addvector(a,b){
    return a.map((e,i) => e + b[i]);
}
addvector([2,3,4],[4,7,90]) # returns [6,10,94]
2 of 7
16

Check out Sylvester. I think it might be what you are looking for.

But if you wanted to implement the objects yourself, then it might be better to do a more OOP approach. JavaScript is a prototype-based language, so it different a little bit from other OOP languages, but its still pretty easy to implement your own prototypes.

Something like:

Vector = function(items) {
    this.items = items
}

Vector.prototype.add = function(other) {
    var result = []
    for(var i = 0; i < this.items; i++) {
        result.push( this.items[i] + other.items[i])
    }

    return new Vector(result);
}

Vector.prototype.subtract = function(other) { /* code to subtract */ }
Vector.prototype.multiply = function(other) { /* code to multiply */ }

And then use them like this:

var a = new Vector([1,2,3]);
var b = new Vector([5,0,1]);

var result = a.add(b)
result.items // [6,2,4]

Or if you wanted to, you could also extend the Array class with some functions with

Array.prototype.vectorAdd = function(other) { /* code to add another array as a vector */ };

And call that using

[1,2,3].vectorAdd([5,0,1])

Hopefully, that might give you a starting point to make your code a little more readable.

Just another note: Unfortunately in this case, JavaScript doesn't support operation overloading so you can't do neat stuff like a+b. You'll have to do something like a.add(b). but as long you return an appropriate object you can chain methods together. Like:

a.add(b).multiply(c).subtract(d);

ps. the presented code might be a little "off", I just typed it up off the top of my head, so treat it more like pseduocode :)

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-work-with-vectors-in-mathjs
How to Work with Vectors in MathJS? - GeeksforGeeks
July 23, 2025 - Vectors in MathJS are represented as arrays. We can create a vector by simply defining an array of numbers. Example: This script creates and displays two vectors, vector A and vector B, using arrays in JavaScript, and logs them to the console ...
🌐
Medium
medium.com › @geekrodion › linear-algebra-vectors-f7610e9a0f23
Linear Algebra with JavaScript: Vectors | by Rodion Chachura | Medium
October 28, 2020 - This is part of the course “Linear Algebra with JavaScript”. ... Vectors are the precise way to describe directions in space. They are built from numbers, which form the components of the vector.
Top answer
1 of 2
10

Every vector is defined by to values eg. x and y. Length of the vector is given by equation length = sqrt(x^2+y^2). Operation of obtaining unit vertor is called normalization. As you wrote, in order to normalize vector, we divide each vector component by length.

Here's an example of implementation in JavaScript. First of all, you need to define a vector somehow. We'll create new object called Vector for that. Then we'll add a function that calculates the length and normalizes the x and y values:

class Vector {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  } 

  get length() {
    const { x, y } = this;
    return (x**2 + y**2)  ** 0.5;
  }
    
  normalize() {
    const { length } = this;
    this.x /= length;
    this.y /= length;
    return this;
  }
}

// create an instance of Vector:
const v = new Vector(2,4);
console.log(v);

// normalize it:
const n =  v.normalize();
console.log(n);

2 of 2
2

Using class syntax, separating the different steps in distinct methods, and treating vectors as immutable, you could do this:

class Vector {
    constructor(x, y) {
        this.x = x;
        this.y = y;
        Object.freeze(this);
    }
    norm() {
        return Math.hypot(this.x, this.y);
    }
    scale(scalar) {
        return new Vector(scalar * this.x, scalar * this.y);
    }
    normalize() {
        return this.scale(1 / this.norm());
    }
}

const v = new Vector(2, 4);
console.log(v.normalize());

If we go for an array representation, and support higher dimensions, it could become something like this:

class Vector extends Array {
    norm() {
        return Math.hypot(...this);
    }
    scale(scalar) {
        return this.map(x => scalar * x);
    }
    normalize() {
        return this.scale(1 / this.norm());
    }
}

const v = new Vector(2, 4);
console.log(v.normalize());

🌐
npm
npmjs.com › search
vector math - npm search
Vector2 library for Javascript inspired by Godot and Unity, but for handling the DOM and Canvas or just plain Vector Math calculations.
🌐
GitHub
github.com › DonKarlssonSan › vectory
GitHub - DonKarlssonSan/vectory: JavaScript vector math library
vectory is a JavaScript vector math library written in ES6.
Starred by 42 users
Forked by 3 users
Languages   JavaScript 100.0% | JavaScript 100.0%
🌐
Duke University
sites.math.duke.edu › ~jdr › linalg_js › doc › vector.js.html
vector.js
* * @desc * This is the square root of `this.sizesq`. * * @example {@lang javascript} * Vector.create(3, 4).size; // 5 * * @type {number} */ get size() { return Math.sqrt(this.sizesq); } /** * @summary * Check if this vector is equal to `other`. * * @desc * Two vectors are equal if they have the same number of entries, and all * entries are equal.