Quick Javascript tip: Combining variable assigments

Dec 08 2005

Sometimes when writing a Javascript function you will want to set several variables to have the same value. For example, at the end of a process you may want to zero all the various counters; or when setting up an unobtrusive DOM script you may be hiding several elements at once.

You might be used to doing it like this:

  1. var one = 1;
  2. var two = 2;
  3. var three = 3;
  4. alert (one + two + three); // Result is 6
  5. one = 0;
  6. two = 0;
  7. three = 0;
  8. alert (one + two + three); // Result is 0

But did you know that you can simply string all those variables together to give them all the same value?

  1. var one = 1;
  2. var two = 2;
  3. var three = 3;
  4. alert (one + two + three); // Result is 6
  5. one = two = three = 0;
  6. alert (one + two + three); // Result is 0

It’s a simple feature that occasionally comes in useful.

Filed under: Javascript.

Technorati tags:

Digg this article

Bookmark this article with del.icio.us

Previously: What are you giving him a balm for? It might bite him!

Next: Post Christmas mash-up


Comments

HeroreV
1682 days ago
Web designers are certainly quite different from programmers. A programmer would never in a million years create an article about a such a simple feature of a language. I don’t understand why some web designers feel the urge to write to everybody else whenever they learn about something in a well known standard that many use. It’s like writing an article about a new word they learned.
#1
Matthew Pennell
1682 days ago
If nobody writes about the simple features, how else are others going to learn about them? You’d be surprised how many users don’t know even the most simple things about the tools they use; I had to show a client how to use Ctrl+C and Ctrl+V to copy and paste text recently – he was amazed!

As an aside, it’s rather amusing that any negative commenters I get here are never willing to put their real name or site URL to their words – rather shows a lack of commitment/balls (delete as appropriate)...
#2
HeroreV
1672 days ago
“how else are others going to learn about them?”

Tutorials perhaps? I doubt that many people come to your blog to learn JavaScript. :p

“it’s rather amusing that any negative commenters I get here”

I wasn’t saying that you shouldn’t post such basic things like “With the color property, you can change the color of the text! Wow!” I was just commenting on the fact that many web designers do that but no programmers (that I know of) do that. Don’t you think that’s interesting?

“are never willing to put their real name or site URL to their words”

I never go by my real name on the internet. If somebody wanted to know more about me, my real name would lead them nowhere. And I don’t have a site to give a URL to. I only keep up on this stuff for fun, and I have nothing to say. I do have a few little pages on my computer that I upkeep that take advantage of XSLT, clean validating XHTML 1.1, and advanced CSS, but it is visually ugly. I have no talent as far as making a page look pretty. I have a xanga if you really want to take a look, but it’s also quite ugly. (xanga.com/herorev)

If you’re looking for positve comments, I will say that your site is quite beautiful, can handle large text, and uses somewhat clean markup (except for stuff like that hilarious #bamboo div). You’ve also had some good content.

However, you have the most irritating comment box I’ve ever used. It may look really nice, but it is way too narrow and the preview in the main section keeps expanding the page so that I have to scroll down several times to finish writing.

“I had to show a client how to use Ctrl+C and Ctrl+V to copy and paste text recently – he was amazed!”

Gawd, I know. Even people that have been using computers for ages don’t know the most basic stuff like that. I just don’t get non-computery people sometimes.
#3
Matthew Pennell
1672 days ago
While it is true that people are unlikely to be visiting specifically to learn Javascript (although the various JS articles are among the most visited on the site), useful tips like this are handy for those designers who do not use much Javascript (and do not actively seek our tutorials) but still like to pick up little tips here and there.

Perhaps if programmers did post simple tutorials like this about the basics of their chosen language, they would encourage more people to learn – I’m more of a developer than a designer, so I’ll read the complex stuff, but a lot of designers will just give up on scripting if it seems too difficult.

I know this comment box is not the most usable – it was supposed to scroll with the page, but I broke the script and never got around to fixing it again…
#4
Matthijs
1668 days ago
Matthew, please keep up the great work. I totally disagree with Herore. For people like me with little or no knowledge of javascript, small tips like these are very welcome. Maybe more then 50% of what I learn is from tips, articles, tutorials or forums online. Sometimes it’s good to study a 10 page tutorial, other times it is usefull to read some short but sweat tips. Often shorter articles which focus on one specific tip are better to remember then a long and hard to digest article.

If some tip is not usefull for someone, the back button is one click away. No problem at all, isn’t it?
#5
Matthew Pennell
1668 days ago
Thanks for your comments, Matthijs – I definitely agree that it’s easier to remember the one useful thing in a short article, than 101 complicated things from a long tutorial. :)
#6