14

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...

31415926
  • 3,811
  • 7
  • 47
  • 78
  • 5
    No. Javascript is dynamically typed. You could look at something like [typescript](http://www.typescriptlang.org/) which can be statically typed and will compile to vanilla javascript (the type checking only happens at compile however). – Matt Burland Apr 14 '14 at 13:27
  • 1
    What do you need that for? If you're looking into type safety, have a look at http://www.typescriptlang.org/ – Bergi Apr 14 '14 at 13:28
  • 1
    [Typed arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays) may be available, depending on the browsers you target. However, they cannot be of *any* type. – Frédéric Hamidi Apr 14 '14 at 13:31

3 Answers3

9
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
Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
Bellash
  • 7,560
  • 6
  • 53
  • 86
  • This works when calling `push` to add a value to the dynamic array. What if you want to prevent assignment of the wrong type, however? Say, `numbers[0] = 0; numbers[1] = 2` to assign the first two, then throw an exception if someone tries to do `numbers[2]='hello'` for example? – Trashman Apr 05 '22 at 19:03
2

Array of specific type in typescript

export class RegisterFormComponent 
{
     genders = new Array<GenderType>();

     loadGenders()
     {
        this.genders.push({name: "Male",isoCode: 1});
        this.genders.push({name: "FeMale",isoCode: 2});
     }

}

type GenderType = { name: string, isoCode: number };    // Specified format
Rahul Uttarkar
  • 3,367
  • 3
  • 35
  • 40
0

If simply you want to restrict user to push values as per first value entered you can use below code

var stronglyTypedArray =  function(type) {
this.values = [];
this.typeofValue;
this.push = function(value) {
   if(this.values.length === 0) {
     this.typeofValue = typeof value;
     this.pushValue(value);
     return;   
   }
    if(this.typeofValue === typeof value) {
       this.pushValue(value);
    } else {
        alert(`type of value should be ${this.typeofValue}`)
    }
}

this.pushValue = function(value) {
     this.values.push(value);
}

}

If you want to pass your own type, you can customize the above code a bit to this

var stronglyTypedArray =  function(type) {
this.values = [];
this.push = function(value) {
    if(type === typeof value) {
       this.pushValue(value);
    } else {
        alert(`type of value should be ${type}`)
    }
}

this.pushValue = function(value) {
     this.values.push(value);
}

}

Bhuwan Pandey
  • 514
  • 1
  • 6
  • 19