spacer

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

home / programming / javascript / diaries / 3

[next]

Information Technology Auditor (PA)
Next Step Systems
US-PA-Wayne

Justtechjobs.com Post A Job | Post A Resume
Developer News
Google Chrome Playing Catch-Up on Extensions
Open Solutions Alliance Gets New Leadership
Red Hat Spacewalk Expands Linux Management

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.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

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

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Review: Lenovo ThinkPad SL300 · OCZ PC3-10666 Gold 2x1GB Review · Apple Recommends Antivirus for Macs

Created: May 13, 2005

URL: