doubt: javascript array

0 views
Skip to first unread message

maya

unread,
Nov 28, 2007, 9:30:39 AM11/28/07
to Principles of Programming
Hai ,
If a=[1,2,3] then how to insert 4 into this array?
What are the methods used in an array?

In Scheme, we have methods like " list?, number? " etc. In javascript,
is there any methods like this?

Venkatesh Choppella

unread,
Nov 29, 2007, 2:41:43 AM11/29/07
to popjs-20...@googlegroups.com
Hello Maya,

Whenever you see a datatype, like arrays, lists, etc., you must ask
yourself the following question: what is the interface, i.e., set of
functions that this datatype provides that let me interact with this
datatype.

For example, as far as I know, neither scheme vectors nor Javascript
arrays provide you a function that 'inserts' an element in this array.
In any case, you also need to clearly specify "insert at which
position". The best way to achieve this seems to be to create a *new*
array. In your example the new array would be [1, 2, 4, 3]. Here 4
is "inserted" in position number 2 (counting from 0).

Javascript has a function called typeof, which takes a javascript
object and returns a string indicating the type.

typeof(4) => "number"
typeof([1, 2, 3]) => "object"


Best Regards.
- venkatesh

--
Venkatesh Choppella, PhD.
Associate Professor
Indian Inst. of Info. Tech. & Mgmt -- Kerala
Park Centre, Technopark, Kariavottam P.O.
Trivandrum, Kerala 695 581, INDIA
home: http://www.iiitmk.ac.in/~choppell
calendar: http://www.google.com/calendar/embed?src=chop...@gmail.com

Reply all
Reply to author
Forward
0 new messages