Link to home
Create AccountLog in
Avatar of Pradip Shenolkar
Pradip ShenolkarFlag for India

asked on

How to get element at particular position from javascript array ?

I have a array in javascript
var cars=["BMW","Mercedes","Audi"];

Open in new window

i want to get the element at particular position from this array.
Is there any function to achieve this ?
i want something which behaves like ArrayList.get() function in java.
arraylist_name.get(2);

Open in new window

Off-course I can't use cars[0],cars[1].... etc.
Avatar of Rob
Rob
Flag of Australia image

var cars=["BMW","Mercedes","Audi"];

console.log(cars[0]); // outputs "BMW"
console.log(cars[1]); // outputs "Mercedes"
console.log(cars[2]); // outputs "Audi"

my article touches on some of this stuff: https://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_13138-Javascript-is-just-an-Object.html
Avatar of Pradip Shenolkar

ASKER

My requirements are such that I can't use cars[0], cars[1] etc
Is there any inbuilt function to do so ?
Nope... that is how you access arrays in javascript, what makes you think otherwise?  What are you trying to do that makes you have to try and access an array like this?
ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
thanks