Home arrow JavaScript arrow Page 2 - 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 - Arithmetic Operators
(Page 2 of 6 )

We've been working with Arithmetic operators since we were children. In JavaScript they work pretty much the same way they did then. Observe:


<html>

<body>

<script type="text/Javascript">


var youweigh = 200

var yourmommaweighs = 400

var linebreak = "<br/>"


document.write("Your weight: ")

document.write(linebreak)

document.write(youweigh)

document.write(linebreak)

document.write("Your mother weighs: ")

document.write(linebreak)

document.write(yourmommaweighs)

totalweight=youweigh + yourmommaweighs

document.write(linebreak)

document.write("Your total weight is: ")

document.write(linebreak)

document.write(totalweight)

</script>

</body>

</html>

The above code insults your mother. Or maybe it doesn't. I mean if she weighs more than four hundred pounds, I guess it could be considered a compliment. At any rate, the above code assigns two variables a value. First, a variable named youweigh is assigned the value 200. Then a variable named yourmommaweighs is assigned the value 400. The program then goes through a process of printing out first the amount you weigh, and then the amount your mother weighs. Then, at the end, it uses the + operator to add the two weights, and print out your combined weight. The result of running the above code:

  Your weight:

  200

  Your mother weighs:

  400

  Your total weight is:

  600

A Note about the Modulus

For a while I found the Modulus to be confusing. No one ever adequately described what it did or what it was, despite the fact that it is very simple to define. Basically, modulus returns the remainder in division. Like so:


<html>

<body>

<script type="text/Javascript">


var valueone = 5

var valuetwo = 2

var linebreak = "<br/>"


modresult = 5 % 2

document.write(modresult)

</script>

</body>

</html>

The above code takes the variable valueone, assigns it the value of five, then takes the variable valuetwo and assigns it the value of two. Finally, we store the modulus in modresult, and print it. Since 5 / 2 equals 2 with a remainder of 1, we end up printing the 1 (the remainder or modulus).


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 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials