Difference between revisions of "JavaScript Operators"

From Techotopia
Jump to: navigation, search
(Assignment Operators)
(Assignment Operators)
Line 12: Line 12:
 
== Assignment Operators ==
 
== Assignment Operators ==
  
In [[Introducing JavaScript Variables]] we looked at the '=' assigmnent operator. This is the most basic of assigmnet operators and serves to simply assign a value to a variable. For example y = 10 assigns the value of 10 to a variable called y.  
+
In [[Introducing JavaScript Variables]] we looked at the '=' assignent operator. This is the most basic of assignment operators and serves to simply assign a value to a variable. For example y = 10 assigns the value of 10 to a variable called y.  
  
The '=' assignment operator does not make any changes to the value before it is assigned to the variable. A number of assigment operators are available, however, that perform arithmetic on the value before assigning it to the variable. These are essentially combined arithmetic and assigment operators. The most common operators of this type, and their "long hand" equivalents are shown below:
+
The '=' assignment operator does not make any changes to the value before it is assigned to the variable. A number of assignment operators are available, however, that perform arithmetic on the value before assigning it to the variable. These are essentially combined arithmetic and assignment operators. The most common operators of this type, and their "long hand" equivalents are shown below:
  
 
<table border="1">
 
<table border="1">
Line 32: Line 32:
 
</table>
 
</table>
  
These combined operators essentially provide a short way of assigning the results of arithmetic expressesions between two variables or a variable and a value and having the result assigned to the first variable.
+
These combined operators essentially provide a short way of assigning the results of arithmetic expressions between two variables or a variable and a value and having the result assigned to the first variable. For example:
 +
 
 +
<pre>
 +
var x = 10;
 +
x += 5; // Assigns a value of 15 to variable x (the same as x = x + 5)
 +
 
 +
var y = 20;
 +
y -= 10;  // Assigns a value of 10 to variable y (the same as y = y - 10)
 +
 
 +
var x = 10;
 +
var y = 5;
 +
 
 +
x /= y; // Assigns a value of 2 to variable x (the same as x = x / y)
 +
 
 +
</pre>
 +
 
 +
<pre>

Revision as of 18:43, 19 April 2007

JavaScript contains a wide range of operators many of which are used frequently by the typical web designer and some that are not. The purpose of this chapter is to cover the most common operators that a web designer will need in the day to day work of building dynamic web pages.

The key operators for those looking to learn the essentials of JavaScript fall into six categories as follows:

  • Assignment Operators
  • Arithmetic Operators
  • Comparison Operators
  • String Operators
  • Conditional Operators
  • Boolean Operators

Assignment Operators

In Introducing JavaScript Variables we looked at the '=' assignent operator. This is the most basic of assignment operators and serves to simply assign a value to a variable. For example y = 10 assigns the value of 10 to a variable called y.

The '=' assignment operator does not make any changes to the value before it is assigned to the variable. A number of assignment operators are available, however, that perform arithmetic on the value before assigning it to the variable. These are essentially combined arithmetic and assignment operators. The most common operators of this type, and their "long hand" equivalents are shown below:

Combined OperatorEquivalent
x += yx = x + y
x -= yx = x - y
x /= yx = x / y
x *= yx = x * y
x %= yx = x % y

These combined operators essentially provide a short way of assigning the results of arithmetic expressions between two variables or a variable and a value and having the result assigned to the first variable. For example:

var x = 10;
x += 5; // Assigns a value of 15 to variable x (the same as x = x + 5)

var y = 20;
y -= 10;  // Assigns a value of 10 to variable y (the same as y = y - 10)

var x = 10;
var y = 5;

x /= y; // Assigns a value of 2 to variable x (the same as x = x / y)