spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / diaries / 3

[next]

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


The JavaScript Diaries: Part 3

  1. Introduction
  2. Data Types & Variables
  3. Operators
  4. Functions
  5. Conditional Statements and Loops
  6. Objects
  7. Browser-Based Objects
  8. Window Methods
  9. Window Event Handlers
  10. Navigator, Screen, History and Location Objects
  11. Arrays: Part 1 - Introduction
  12. Arrays: Part 2 - Multiple Array Types
  13. Arrays: Part 3 - Array Properties and Methods
  14. The Math Object
  15. The Date Object

[Answers for last installment]

JavaScript Operators

In this installment we'll take a look at JavaScript operators, which are used to accomplish many different tasks. This may be a review for some of you; though others you may want to study this subject in more depth.

An operator is a tool used to manipulate data. It can perform mathematical calculations, data comparisons, and assign data values to variables. Generally, operators are represented by mathematical symbols but in a few cases they are actual words. The term "operator" is derived from the action that is performed on a piece of data. The action is called an "operation:" the tool used to perform the operation is called an "operator." The values (i.e., numbers assigned to a variable) that are used in the operation are called "operands" (I'll use these two terms — value and operand — interchangeably).

The most common operators used in JavaScript can be divided into six different categories:

  • Mathematical: Used to perform mathematical calculations.
  • Comparison: Used to compare two values, variables or statements.
  • Assignment: Used to assign values to variables.
  • Logical: Used to compare two conditional statements to see if one or both are true.
  • Bitwise: Used to perform logical operations on the binary ("bit") level.
  • Special: Used for specific tasks, these operators can also help eliminate unnecessary code.

As we discuss these operators below, go ahead and try out the code using the template that we made last time. This way you'll see exactly what the code is doing. Remember also to type them in yourself as that will help you when you write your own. As we go through each example, try to figure out what is happening before you read the explanation.

Mathematical Operators

Operator Name Description
+ Addition Adds two values together
- Subtraction Subtracts one value from the other
* Multiplication Multiplies two values
Division Divides one value by another
% Modulus Divides one value by another and returns the remainder
++ Increment Adds 1 to a value
-- Decrement Subtracts 1 from a value
- Negation Changes the sign of the value [Makes a positive value negative and a negative value positive.]

Addition Operator (+)

This operator is used to add together numbers or strings.

  var theSum=2+5;
  alert(theSum);

In this script we are declaring a variable called theSum and initializing it with a value of 2+5. Next we call an alert window to display the results of the variable. This would result in an alert window displaying the number 7. Go ahead and try it.

  var myNum=2;
  var yourNum=5;
  var theSum=myNum+yourNum;
  alert(theSum);

This script declares and initializes three variables, myNum, yourNum, and theSum. The first two are given the values of 2 and 5, respectively. The third variable is a calculation, adding the first and second variables. Next, the script calls an alert window to display the results of the third variable, theSum. This would result in an alert window with the number 7. This script is basically the same as the previous one except that we can change the value of each variable individually.

  var theSum=5;
  var newNum=theSum+3
  alert(newNum);

In this last example, we declare and initialize two variables, theSum and newNum. The first variable is given a value of 5. In the second variable, a value of 3 is added to the value of the first variable. An alert window is then called to display the results of the second variable, newNum, which produces the value of 8.

Subtraction Operator (-)

This operator is used to subtract the value on the right of the operator from the value on the left, i.e., "7 - 2" would read "seven minus two".

  var newNum=7;
  var newNum2=2;
  var result=newNum-newNum2;
  alert(result);

This script declares and initializes three variables, newNum, newNum2, and result. The first two are given the value of 7 and 2, respectively. The third variable, result, is a calculation, subtracting the variable newNum2 from the variable newNum. An alert window is then called to display the results of the third variable, result, which produces a value of 5.

home / programming / javascript / diaries / 3

[next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: May 13, 2005

URL: