vinay reddy
vinay reddy

Reputation: 7

how to get an single element form an array : javascript

I have array with values like this

arr = [ 'java', 6 ]

My question, is how do I get just "java" in my output

My output should be like

java

Upvotes: 0

Views: 187

Answers (3)

Trivaxy
Trivaxy

Reputation: 141

Javascript arrays are have their indexes start at 0. So, arr = [ 'java', 6 ] contains 2 elements. And since arrays have their index start at 0, 'java' would be at the [0] index and '6' would be at the [1] index. To access it you just need to do arr[0] and output it with, for example, console.log(arr[0]) which is the simplest way to access 'java'. (e.g You can do arr[i] where i is a number with a value of 0)

Upvotes: 0

mody
mody

Reputation: 65

if you want you output java this is sol :

var arr = [ 'java', 6 ];
alert(arr[0]);

Upvotes: 0

hmatar
hmatar

Reputation: 2429

Use indexing; 0 for 'java', and 1 for 6. To get 'java', just do arr[0]

Upvotes: 1

Related Questions