Home arrow JavaScript arrow Page 3 - JavaScript Operators
JAVASCRIPT

JavaScript Operators


You can't do much with a programming language without operators. JavaScript is no exception. So this article will introduce you to JavaScript operators and what you can do with them.

Author Info:
By: James Payne
Rating: 4 stars4 stars4 stars4 stars4 stars / 3
November 05, 2007
TABLE OF CONTENTS:
  1. · JavaScript Operators
  2. · Arithmetic Operators
  3. · Assignment Operators
  4. · Add and Assign
  5. · Comparison Operators
  6. · Logical Captain?

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
JavaScript Operators - Assignment Operators
(Page 3 of 6 )

We worked with the "=" assignment operator when we went over variables. As the name suggests, assignment operators assign a value. Where they can get tricky is in the pre/post area. When you use an assignment variable in the position before the variable, it is added prior to the line being executed. When you use it after the variable, it is added afterward. Sound confusing? This should clear it up.

<HTML>

<HEAD>

<script type="text/Javascript">

a=10

b=6

document.write('Angelina Jolie is '+a+++', and in ten years will be an '+a)

document.write('<BR>Jennifer Lopez is a '+--b+', and in ten years will be a '+b)

</SCRIPT>

</HEAD>

</HTML>

In the above sample I rated the current hotness and predicted future hotness of Angelina Jolie and Jennifer Lopez. As you can see, I don't think J-Lo is very hot. I used the increment (++) operator to increase the hotness of Angelina Jolie by one. Since the ++ appeared after the variable name, it prints 10 instead of eleven in the first part of the sentence, then it increments the value by one, and prints eleven at the end of the sentence. This is the result:

  Angelina Jolie is 10, and in ten years will be an 11
  Jennifer Lopez is a 5, and in ten years will be a 5

If I had put the ++ at the beginning of the variable, it would have read this way:

  Angelina Jolie is 11, and in ten years will be an 11
  Jennifer Lopez is a 5, and in ten years will be a 5

It works the same way with the - decrementing operator. As you can see in the J-Lo example, it subtracted 1 from the value of b prior to printing the variable.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials