I am a very mathematically oriented programmer, and I happen to be doing a lot of java script these days. I am really disappointed in the math aspects of javascript:

  • the Math object is almost a joke because it has so few methods
  • you can't use ^ for exponentiation
  • the + operator is very limited, you cant add array's of numbers or do scalar multiplication on arrays

Now I have written some pretty basic extensions to the Math object and have considered writing a library of advanced Math features, amazingly there doesn't seem to be any sort of standard library already out even for calculus, although there is one for vectors and matricies I was able find.

The notation for working with vectors and matricies is really bad when you can't use the + operator on arrays, and you cant do scalar multiplication. For example, here is a hideous expression for subtracting two vectors, A - B:


I have been looking for some kind of open-source project to contribute to for awhile, and even though my C++ is a bit rusty I would very much like to get into the code for V8 engine and extend the + operator to work on arrays, to get scalar multiplication to work, and possibly to get the ^ operator to work for exponentiation. These things would greatly enhance the utility of any mathematical javascript framework.

I really don't know how to get involved in something like the V8 engine other than download the code and start working on it. Of course I'm afraid that since V8 is chrome specific, that without browser cross-compatibility a fundamental change of this type is likely to be rejected for V8. I was hoping someone could either tell me why this is a bad idea, or else give me some pointers about how to proceed at this point to get some kind of approval to add these features.


  • Is the desire for this added functionality for your own local javascript use, your own javascript pages that will render only in one browser, your own local javascript pages that must work in all popular browsers, a desire to improve javascript for all?
    – jfriend00
    Nov 18, 2012 at 22:11
  • It would certainly be for the benefit of all. I think having a concise notation for basic linear algebra is an important feature that can greatly improve code readability. Nov 18, 2012 at 22:13
  • 1
    That's the hardest way to go. To get something added to all browser's javascript engines, you would need to build momentum behind a ECMAScript standard's effort since this stuff is pretty much driven by standard's bodies now. You could start with an library that could extend the Math object (there are many out there already), though that isn't going to add new operators like ^ to the language.
    – jfriend00
    Nov 18, 2012 at 22:24
  • 1
    Perhaps the closest you can get to the desired syntax is to create a prototype with methods like + and ^ and call them as A['+'](B) and C['^'](D). OTOH beware seeing your code on the DailyWTF if you take this approach. Nov 19, 2012 at 8:03
  • Never tried it but doesn't Numbers.js offer calc functions? Nov 15, 2013 at 21:09

2 Answers 2


Your code isn't too likely to find its way into V8. It's a language change, and language changes happen only rarely, and if they do, it's usually because a company, rather than a person or even a community, is pushing for them.

So ... unless you want to train your VM muscles, I'd try and stay within the language.


You've hit on the two things that actually bum me out about JS sometimes. Wimpy numbers and no operator overloading. One of JS's strengths however is ease of creating a less tedious interface than the perhaps more verbose/self-documenting interfaces from core functionality (or any icky interface you don't want to deal with for that matter). Here's a simple chaining addition interface I whipped up. I think I'd rather just add a subtract method and chain as necessary but when it sees a '-' it will subtract the next vector arg and its args are also overloaded to handle dropping an interface vector object directly in so stuff like this should work:

var vec1 = $v([1,2,3]);

vec1.add([1,1,1],[1,1,1]).val();// [3,4,5]
vec1.add('-',[2,2,2]).val();// [1,2,3]

var vec2 = $([3,2,1]).add(vec1,[1,1,1],'-',[2,2,2]).val(); // [5,5,5]

And the func:

function $v(vector){ //produce basic chaining add/subtract wrapper

    return new VectorInterface(vector);

    function VectorInterface(vectorVal){

            vectorVal = [0,0,0]; //no idea if you would want this as a default
        //vectorVal is persistent

        //I use function hoisting to keep interface declaration clean
        this.val = val;
        this.add = add;
        //this.subtract = subtract; //so we can subtract without cheesy '-' args later

        //public methods

        function val(){
            return vectorVal;

        function add(){
                argLen = arguments.length,
                mod = 1

            for(var i=0; i < argLen; i++){

                var thisArg = arguments[i];

                if(thisArg.constructor.name === VectorInterface){
                    thisArg = thisArg.val();

                    vectorVal[0]+= mod * thisArg[0];
                    vectorVal[1]+= mod * thisArg[1];
                    vectorVal[2]+= mod * thisArg[2];
                    mod = 1;
                else if(thisArg='-'){ 
            return this; //make it chainable for addition of other methods

        function validateVector(vec){
            if((vec !== undefined) && (vec.length !== undefined) && ( vec.length === 3 )){
                return true;
            else { return false; }


It wouldn't be that hard to add other operator methods or further overload the args to identify arrays of array vectors and chew through them as appropriate when added to the args.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.