tags:

views:

124

answers:

6

I am using the following JavaScript code:

var a = [23, 34, 45, 33];

Is a considered an array of integers?

+10  A: 

Yes, a is an array. However, since Javascript isn't statically typed, it can contain other types as well, such as strings, objects, other arrays and so on. Therefore, tagging it as "an array of integers" wouldn't be right.

Eli Bendersky
A: 

Yes, it is. It really is.

Marcelo Cantos
@Marcelo: just curios, did you add "it really is" only to fight off the minimum 15-char limit :-) ?
Eli Bendersky
Yes, I did. Truly.
Marcelo Cantos
@Marcelo, I hate that restriction :-) It sucks that they don't remove it at least for members above some rating. ++ on the pun
Eli Bendersky
I agree, but it's a minor inconvenience, I guess.
Marcelo Cantos
h​​​​​​​​​​​​mm
bobince
@bobince: how did you do that ?
Eli Bendersky
A Unicode magician never reveals his methods ;-)
bobince
Ah, but you are so easily unmasked by Firebug. Nice trick!
Marcelo Cantos
A: 

Javascript supports var so you can have combination of string, int, decimal here.

To check further details see this link http://javascript.blogsome.com/category/1/javascript-array/

Ravia
I think this is misleading. Is there a necessary correlation between the keyword `var` and the fact that arrays are not statically typed?
Chris Farmer
Not at all. `var` declares a variable against the current scope (mostly local). This is totally independent of arrays, whatever they contain.
Marcel Korpel
+3  A: 

JavaScript doesn't have an Integer type. It is an Array containing Numbers (but not limited to containing only Numbers)

David Dorward
A: 

Interesting question...

a is considered an array of integers as long as you want it that way since javascript is dynamically typed, ie. you could potentially do:

var a = [23, 34, 45, 33]; alert(a[0] + 1); // shows 24

a = "sometext"; alert(a + 1); // shows "sometext1"

HTH.

Sunny
+1  A: 

You are creating an array a using an array literal.

An array literal is a list of zero or more expressions, each of which represents an array element, enclosed in square brackets ([]). When you create an array using an array literal, it is initialized with the specified values as its elements, and its length is set to the number of arguments specified.

As the other answers already pointed out, JavaScript arrays are able to contain elements of different data types.

Daniel Vassallo