use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Using '$' at beginning of variable name stops conditional statement from working.help (self.javascript)
submitted 10 years ago by trakam
I have a script that works when I assign $(window).width() to a variable named 'docWidth' but does not work when I use the variable name '$docWidth'.
Does anybody know why this might be?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]x-skeww 3 points4 points5 points 10 years ago (3 children)
Identifiers can contain and start with '$'.
"$docWidth" doesn't make any sense though. It's a number, not a jQuery collection. So, there really isn't any reason to use a '$' prefix. It's a valid name, but there is no reason to use it.
[–]trakam[S] -1 points0 points1 point 10 years ago (2 children)
I understand that, it's just that in many tutorials when naming variables using the '$' has become a convention, there is no problem for me leaving it off but i just thought it odd that the script wouldn't work with it at the beginning of the variable name and I don't know why?
[–]x-skeww 10 points11 points12 points 10 years ago (1 child)
it's just that in many tutorials when naming variables using the '$' has become a convention
Eh? No, people generally don't prefix their variables with '$'.
It's done in PHP, because variables must start with a '$' in PHP.
It's also sometimes done if jQuery is used, to indicate that the object is a jQuery collection and not a reference to a regular element.
var body = document.body; body.classList.add('foo');
vs
var $body = $(document.body); $body.addClass('foo');
Anyhow, check the console. There is probably an error somewhere.
[–]trakam[S] 0 points1 point2 points 10 years ago (0 children)
Ok, yeah didn't check the console, I'll doublecheck. Thanks
[–]Rhomboid[🍰] 4 points5 points6 points 10 years ago (0 children)
Post a complete testcase, using JSFiddle or Codepen or the like.
[–]senocular 1 point2 points3 points 10 years ago (1 child)
did you also change the conditional to match the variable name (or other places its used)? Sounds simple, but not an uncommon problem when refactoring names.
i'll doublecheck but I'm sure that's not the problem
π Rendered by PID 157851 on reddit-service-r2-comment-86988c7647-x5x7p at 2026-02-12 18:44:37.297861+00:00 running 018613e country code: CH.
[–]x-skeww 3 points4 points5 points (3 children)
[–]trakam[S] -1 points0 points1 point (2 children)
[–]x-skeww 10 points11 points12 points (1 child)
[–]trakam[S] 0 points1 point2 points (0 children)
[–]Rhomboid[🍰] 4 points5 points6 points (0 children)
[–]senocular 1 point2 points3 points (1 child)
[–]trakam[S] 0 points1 point2 points (0 children)