Category Archives: JavaScript

FAQ: How to do 1 + 1 in JavaScript

The speed and size optimized way

2; //precomputed using abacus technology


As a reusable function pattern

function onePlusOne(){
    return 2 //still very fast
} 

onePlusOne(); //you can do something with this value now, like alert it or better document.write it.


As a configurable function pattern

function plus(a, b){
   return +a + b // no more optimized for speed, but at least you can pass different parameters now
}

plus(1, 1) // this works with any other parameter now like plus(1, 2) but who needs 2 anyways?


The object oriented, prototype way

function Addendum(a){
   this.value = +a
}
Addendum.prototype.add = function(b){
   this.value += b
}
Addendum.prototype.toString = function(){
   return '' + this.value
}
Addendum.prototype.valueOf = function(){
   return +this.value;
}

var addendum = new Addendum(1)
addendum.add(1)
+addendum // basically a standard object oriented way for adding two numbers.


The object oriented, prototype chainable pattern way

function Addendum(a){
   this.value = +a;
}
Addendum.prototype.add = function(b){
   this.value += b;
   return this;
}
Addendum.prototype.toString = function(){
   return '' + this.value;
}
Addendum.prototype.valueOf = function(){
   return +this.value;
}

;+ new Addendum(1).add(1) // you can now add(1).add(1).add(1) until you get bored of it


The MooTools way(TM)

var Addendum = new Class({
   initialize: function(a){
      this.value=+a
   },
   add: function(b){
      this.value+=b
      return this
   }
})

new Addendum(1).add(1).value // or one could implement valueOf if he wants


New MooTools prime way

var Addendum = prime({
   constructor: function(a){
       this.value = +a
   },
   add: function(b){
      this.value += b
      return this
   }
})

new Addendum(1).add(1).value //pretty much identical ...


(One of) the functional way

function add (a) {
   return function (b){
      return +a+b
   }
}

add(1)(1) // this looks like a couple of asses...


Same but working both with full parameters or using a partial invocation / currying way

function add(a, b){
   if(arguments.length == 1){
      return add.bind(null, a);
   }else{
      return a + b
   }
}

add(1, 1) === add(1)(1) // guess what? it's true


Using Monads

// Monad.js MIT License, Cristian Carlesso @kentaromiura mykenta.blogspot.com
function Monad(value){
   if (value instanceof Monad) return value
   this.value = function(){return value}
}

Monad.prototype.bind = function(f){
   var value = this.value()
   if (f === Monad) return this
   if (typeof f === 'function') return new Monad(f(value))
   return this
}

var base = 1;
var transformation = function add1(x){ return +x+1 }

new Monad(base).bind(transformation).value()


By subtracting a negative value

1 - -1 // you can apply the same patterns above if you really want


Same but using auto casting (thanks @Golmote)

!0 - -!0 // this is particolarly useful if your keyboard has the 0 broken
 


@WebReflection 1337 version

var one = 1
one -=- one 


Same but a bit obfuscated

var _ = _ == _
_ -=- _


The inception mode (interpreter inside interpreter)

eval('1+1') //Eviluation!


The ’90 version of above

var One = 1, Plus = '+'
eval([One, Plus, One].join(''))


Using Array unary Math

var a = 1, b = 1

function toUnary(number){
   return Array.apply(null, Array(number)) 
}

function unaryCount(a){
   return arguments
}

with(unaryCount.apply(null, toUnary(a).concat(toUnary(b)))) length


The is it JavaScript or Brainfuck mode (again @Golmote suggestion)

++[[]][+[]]+[++[[]][+[]]][+[]]


Array you’re definitelly too big (thanks @astolwijk)

var a, b = a = 1
[a, b].reduce(function(){ return arguments[0] + arguments[1] }, 0)

Advertisements