/    Sign up×
Community /Pin to ProfileBookmark

difference between "(" and "["

What exactly is the difference between parenthesies and brackets?

Would it be possible to create a javascript function that can be used with brackets to pass params, e.g.

myfunction[var1, var2];

I know that last question is an odd one, but can it be done?!?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@fredmvDec 03.2003 — Parentheses are used to call and define functions, for example, to define a function the following syntax is used:function foo()
{
//...
}
Then, to call that function, you'd do this:foo();The brackets are used to create arrays or reference elements within arrays. You could, for example, create an array like this:var bar = ['foo', 'bar'];Then reference values in it like this:foo[0];
foo[1];
For your last question, it's not exactly possible to make a function that looks exactly like that, but it's possible to create one that acts similar to it. You can create something called an [i]annonymous function[/i] (a function in which has no name) and store it within and array cell. Here's an example:var a = new Array();
a[0] = function() { return "Hello, World!"; }
a[1] = a[0]();
alert(a[1]);
That would then alert the string "Hello, World!" since the first element of the array defines an annoymous function that returns the string "Hello, World!", then, the second element of the array calls the function which results in the return value placed inside of it.
Copy linkTweet thisAlerts:
@ilbonparaurtiauthorDec 03.2003 — How come to use certain DOM related functions, for example docuemtn.getElementbyID[] uses brackets?
Copy linkTweet thisAlerts:
@fredmvDec 03.2003 — It doesn't. You use [font=courier]document.getElementById[/font] like this:document.getElementById("foo");Where [i]foo[/i] is the [font=courier]id[/font] of the HTMLElement object you are referring to.
×

Success!

Help @ilbonparaurti 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.28,
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,
)...