How to declare array of specific type in javascript

31415926 picture 31415926 · Apr 14, 2014 · Viewed 19k times · Source

Is it possible in java script to explicitly declare array to be an array of int(or any other type)?

something like var arr: Array(int) would be nice...

Answer

Bellash picture Bellash · Apr 14, 2014
var StronglyTypedArray=function(){
      this.values=[];
      this.push=function(value){
      if(value===0||parseInt(value)>0) this.values.push(value);
      else return;//throw exception
     };
     this.get=function(index){
        return this.values[index]
     }
   }

EDITS: use this as follows

     var numbers=new StronglyTypedArray();
     numbers.push(0);
     numbers.push(2);
     numbers.push(4);
     numbers.push(6);
     numbers.push(8);
     alert(numbers.get(3)); //alerts 6