logo
g Text Version
Auto
Beauty & Self
Books & Music
Career
Computers
Education
Family
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
Money
News & Politics
Relationships
Religion & Spirituality
Society & Culture
Sports
Travel & Leisure
TV & Movies

dailyclick
Bored? Games!
Postcards
Astrology
Take a Quiz
Rate My Photo

new
Journals
Folklore and Mythology
Business Coach
Marriage
Senior Living
Ethnic Beauty
Adolescence


dailyclick
All times in EST

Autism Spectrum Disorders: 4:00 PM

Full Schedule
g
g JavaScript / Java Site
Editor Wanted
BellaOnline's JavaScript / Java Editor

g

JavaScript Arithmetic Operators
Guest Author - Julie L Baumler

In this article we look at arithmetic operators in JavaScript. Many of the operators are the same as in basic math, so this may seem like a review of elementary school, but some of them may be new to you and it's important to know that you can use them in JavaScript. In the examples below, we mostly use numbers, but in real programs you usually use variables.

Operator
Use
ExampleResult
=
assignment
a = 4
a is given the value 4
*
multiplication
3 * 4
12
/
division
12 / 4
3
%
modulus
13 % 4
1
+
addition
3 + 4
7
-
subtraction
12 – 5
7


All of these except for modulus should be familiar to you from elementary school. Modulus (%) gives the remainder from doing integer division. So 13 % 4 means that you divide 13 by 4, this results in 3 with a remainder of 1. Thus 13 % 4 is 1.

You can also use the addition operator with strings. In this case it concatenates (joins) the two strings together. A common mistake is adding (or forgetting) a space between the two strings you are joining.

Operator
Use
Example
Result
+
addition
"he" + "art"
heart
"he" + " art"
he art


Often you want to add (or subtract, multiply or divide) a variable by some value and then assign the result to the original variable. The assignment operator can be combined with these operators to do this in one step.

ExpressionShortcut
a = a * 3
a *= 3
b = b / 4
a /= 4
c = c % 3
c %= 3
d = d + 5
d += 5
f = f - 2
f -= 2

Happy Scripting!

This site needs an editor - click to learn more!

JavaScript Resources
RSS
Related Articles
Previous Features
Site Map

Add JavaScript+Arithmetic+Operators to Twitter Add JavaScript+Arithmetic+Operators to Facebook Add JavaScript+Arithmetic+Operators to MySpace Add JavaScript+Arithmetic+Operators to Del.icio.us Digg JavaScript+Arithmetic+Operators Add JavaScript+Arithmetic+Operators to Yahoo My Web Add JavaScript+Arithmetic+Operators to Google Bookmarks Add JavaScript+Arithmetic+Operators to Stumbleupon Add JavaScript+Arithmetic+Operators to Reddit


Content copyright © 2009 by Julie L Baumler. All rights reserved.
This content was written by Julie L Baumler. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.

g


For FREE email updates, subscribe to the JavaScript / Java Newsletter


Past Issues


print
Printer Friendly
bookmark
Bookmark
tell friend
Tell a Friend
forum
Forum
email
Email Editor

g features
Hearing Dog Tool - Adding Randomness

Hearing Dog Training Tool – setTimeout()

GCJ - an Independant Java Implementation

Archives | Site Map

forum
Forum
email
Contact

Past Issues
memberscenter

jobs
what
job title, keywords
where
city, state or zip
jobs by job search


vote
Growing a Garden
Veggies and Flowers
Veggies Only
Flowers Only
No Garden

g


| About BellaOnline | Privacy Policy | Advertising | Become an Editor |
Website copyright © 2009 Minerva WebWorks LLC. All rights reserved.


BellaOnline Editor