/    Sign up×
Community /Pin to ProfileBookmark

Need help understanding this syntax

Good day To you all!

It’s been a while since I last posted here. Lately I’m in the need to expand my knowledge about JS. I hope you can help me out with a query I have:

I don’t understand the following Syntax:

[CODE]var employees = responseXML.getElementsByTagName(“employees”)[0];[/CODE]

How does the [0] work here? Why is it at the end of an statement? Why index 0 ?

Those were a lot of questions, but I hope you can tell me the general functionality of this syntax.

Thank you very much !
Best Regards,

Jose

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@scragarApr 07.2009 — [CODE]var employees =

responseXML [b]An Xml object[/b]
.getElementsByTagName("employees") [b]An array of employees[/b]
[0]; [b]The first element of the array[/b]
[/CODE]

So it returns the first employee node.
Copy linkTweet thisAlerts:
@J_LoganauthorApr 08.2009 — Wow, JS Syntax is really weird.

I could have assumed that the use of that syntax was for that purpose if the [0] thing would have been inside the parenthesis.

Anyway,

Thank you very much.

Sorry for the delayed response.

Jose
Copy linkTweet thisAlerts:
@dmboydApr 08.2009 — It's really no different than any other object-oriented language. For example, Python does the same:
harry = 'Harry Bob Joe'.split(' ')[0]

In fact, you can use that code in JavaScript as well, though JavaScript requires a RegExp object rather than a string as the argument to the String.split() function:
var harry = 'Harry Bob Joe'.split(/ /)[0];

Edit: If you want a language that is understandable and has an unusual syntax, check out [url=http://wikipedia.org/wiki/Objective-C]Objective-C[/url]. It's definitely different! :p
×

Success!

Help @J_Logan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...