Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

General questions 1

Status
Not open for further replies.

Geee

Programmer
Apr 23, 2001
253
GB
Hi, anyone who has read my posts will know I'm new to the whole JS scene and I just had a couple of questions...

I've been learning by following tutorials and dissecting others code. I noticed straight away that variable names have no prefix like they do in other languages, but when coding I have automatically slipped a $ into the variable name and it still works. Does this matter, or does the $ cast the variable as a certain type, like string or something?

Is it possible to refer to a variable by a variable? This sound confusing but in PHP I use $$var quite a lot. Is it possible to do anything similar in JS?

Thanks guys.

-Geeeeeeeeeeeeeeeeeeeeeeee-
 
AFAIK, the $ thingie just makes nothing appart from making your code harder to read or maintain.

And no, there isn't any kind of pointer in Javascript.

Cheers,
Dian
 
Is it possible to refer to a variable by a variable?

Yes.

For example:

Code:
var abc = '123;'
alert(abc);  // should show '123';

var someVar = 'abc';
alert(window[someVar]);  // should show '123';

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
AFAIK, the $ thingie just makes nothing appart from making your code harder to read or maintain.

I don't understand how this is the case to be honest. I find using a prefix on a variable makes the code EASIER to read as I can pick out the fact its a variable due to the $ sign. Care to expand a little?

-Geeeeeeeeeeeeeeeeeeeeeeee-
 
I don't understand that rationale, either. It's personal preference.

From the Moz JS 1.5 core guide section on variable names (
A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).

Starting with JavaScript 1.5, you can use ISO 8859-1 or Unicode letters such as å and ü in identifiers. You can also use the \uXXXX Unicode escape sequences listed on page Unicode Escape Sequences as characters in identifiers.

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Great, I shall continue using the good old dollar sign then. Thanks for the help Dan.

-Geeeeeeeeeeeeeeeeeeeeeeee-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top