JavaScript operators look a lot like those in C++ or Java, but with some unusual twists.
JavaScript operators will in general handle any mixture of data types.
Like most programming languages, JavaScript has boolean or "logical" operators.
JavaScript Binary Boolean Operators |
||
Operator |
Definition |
Examples |
|| |
Or |
z = false || true; document.write(z) |
|
||
&& |
And |
z = false && true; document.write(z)
|
|
There is also one unary boolean operator:
JavaScript Unary Boolean Operator |
||
Operator |
Definition |
Examples |
! |
Not |
z = !false; document.write(z) |
|
And, there is an unusual trinary boolean operator, inherited from the C language:
JavaScript Trinary Boolean Operator |
||
Operator |
Definition |
Examples |
?: |
Conditional |
z = true ? 5 : 1; document.writeln(z) |
|
In this operator, if the first parameter is true, the result is the second parameter, and if the first parameter is false, the result is the third parameter.
Although the first parameter of this operator is boolean, the second and third can be any data type.
x = false |
|
Similarly, in the trinary conditional, either the second or third parameter is executed, depending on the value of the first, but not both.
JavaScript Boolean Operator Conversions of Non-Boolean Types |
||
Data Type |
Conversion |
Examples |
Null |
false |
z = !null; document.write(z) |
Number, |
false |
z = !0.0; document.write(z) |
Number, |
true |
z = !32.7; document.write(z) |
|
||
String, |
false |
z = !""; document.write(z) |
String, |
true |
z = !"maybe"; document.write(z) |
JavaScript has the standard set of numeric operators, and a few others, too.
JavaScript Binary Numeric Operators |
||
Operator |
Definition |
Examples |
+ |
Addition |
z = 5 + 2.1; document.write(z) |
|
||
+= |
Addition-Assignment |
z += 1.7; document.write(z) |
|
||
- |
Subtraction |
z = 5 - 2.1; document.write(z) |
|
||
-= |
Subtraction-Assignment |
z -= 1.7; document.write(z) |
|
||
* |
Multiplication |
z = 5 * 2.1; document.write(z) |
|
||
*= |
Multiplication-Assignment |
z *= 1.7; document.write(z) |
|
||
/ |
Division |
z = 5 / 2.1; document.write(z) |
|
||
/= |
Division-Assignment |
z /= 1.7; document.write(z) |
|
||
% |
Modulus |
z = 5 % 2.1; document.write(z) |
|
||
%= |
Modulus-Assignment |
z %= 1.7; document.write(z) |
|
Note that the assignment operators, like an ordinary assignment, can only be used with variables on the left, which are changed to the result of the operation.
Note also that, like an ordinary assignment, the variable on the left may have its type changed:
z = true z += 1 document.write(z)
|
|
JavaScript also provides a few unary numeric operators, including the increment/decrement-assignment operators introduced in the C language:
JavaScript Unary Numeric Operators |
||
Operator |
Definition |
Examples |
|
Negation |
document.write(-z) |
|
||
++ |
Prefix Increment-Assignment |
document.writeln(++z); document.write(z) |
|
||
++ |
Postfix Increment-Assignment |
document.writeln(z++); document.write(z) |
|
||
|
Prefix Decrement-Assignment |
document.writeln(--z); document.write(z) |
|
||
|
Postfix Decrement-Assignment |
document.writeln(z--); document.write(z) |
|
The prefix and postfix increment/decrement operators have the same effect on their operand, but the assignment will take place before or after, respectively, the variable is used in an enclosing statement.
The conversions that occur when non-numeric data types are used with numeric operators are summarized in this table:
JavaScript Numeric Operator Conversions of Non-Numeric Types |
||
Data Type |
Conversion |
Examples |
Null |
0 |
z = -null; document.write(z) |
|
||
Boolean, false
|
0 |
z = -false; document.write(z) |
|
||
Boolean, true
|
1 |
z = -true; document.write(z) |
|
||
String, |
0 |
z = -""; document.write(z) |
|
||
String, |
The number represented |
z = -"2.3"; document.write(z) |
|
||
String, |
Not-a-Number |
z = -"2.3maybe"; document.write(z) |
|
There is one exception to the above, when strings are used with the "+" operator, to be described shortly.
Occasionally integers may need to have their bits manipulated, and JavaScript provides the necessary tools.
JavaScript Binary Bitwise Operators |
||
Operator |
Definition |
Examples |
| |
Or |
z = 5 | 3 // 0b101 | 0b011 |
|
||
|= |
Or-Assignment |
z |= 9 // z |= 0b1001 |
|
||
^ |
Exclusive Or |
z = 5 ^ 3 // 0b101 ^ 0b011 |
|
||
^= |
Exclusive Or-Assignment |
z ^= 9 // z ^= 0b1001 |
|
||
& |
And |
z = 5 & 3 // 0b101 & 0b011 |
|
||
&= |
And-Assignment |
z &= 9 // z &= 0b1001 |
|
||
<< |
Left Shift |
z = 5 << 3 // 0b101 << 3 |
|
||
<<= |
Left Shift-Assignment |
z <<= 9 // <<= 9 |
|
||
>> |
Right Shift |
z = 5 >> 3 // 0b101 >> 3 |
|
||
>>= |
Right Shift-Assignment |
z >>= 9 // >>= 9 |
|
There is also one unary bitwise operator:
JavaScript Unary Bitwise Operators |
||
Operator |
Definition |
Examples |
~ |
Bitwise Not |
z = 5 // 0b101 |
|
Note from this last result that JavaScript assumes a two's-complement representation of negative numbers, i.e. 6 = 0b110 but 6 = 0b11...11010.
With the bitwise operators, JavaScript always uses a 32-bit integer representation.
If an operand of a bitwise operator is not an integer, it's converted first to a number as described above, if necessary, and then to an integer via truncation:
z = "5.7" << 1 // "5.7" -> 5.7 -> 5 = 0b101, << 1 document.write(z) |
|
Joining strings is frequent enough in JavaScript to benefit from its own operator.
JavaScript Binary String Operators |
||
Operator |
Definition |
Examples |
+ |
Addition |
z = "Hello, " + "World"; |
|
||
+= |
Addition-Assignment |
z = "Hello, "; z += "World"; |
|
If you operate on a string and another data type, the latter will first be converted to a string:
JavaScript Concatenation Operator Conversions of Non-String Types |
||
Data Type |
Conversion |
Examples |
Null |
"null" |
z = "This is " + null; |
|
||
Boolean |
"false" |
z = "This is " + false; |
|
||
Number |
A numeric representation |
z = "This is " + 2.3; |
|
Question: how might you convert a number to a string?
Question: how might you convert a string to a number?
Comparison of two values is very common when programming, but in JavaScript it may not always be what you expect.
JavaScript Binary Numeric Comparison Operators |
||
Operator |
Definition |
Examples |
== |
Equal |
z = 5 == 4; document.write(z) |
|
||
!= |
Not Equal |
z = 5 != 4; document.write(z) |
|
||
> |
Greater than |
z = 5 > 4; document.write(z) |
|
||
>= |
Greater than or Equal to |
z = 5 >= 4; document.write(z) |
|
||
< |
Less than |
z = 5 < 4; document.write(z) |
|
||
<= |
Less than or Equal to |
z = 5 <= 4; document.write(z) |
|
JavaScript Binary String Comparison Operators |
||
Operator |
Definition |
Examples |
== |
Equal |
z = "College" == "Collage"; document.write(z) |
|
||
!= |
Not Equal |
z = "College" != "Collage"; document.write(z) |
|
||
> |
Greater than |
z = "College" > "Collage"; document.write(z) |
|
||
>= |
Greater than or Equal to |
z = "College" >= "Collage"; document.write(z) |
|
||
< |
Less than |
z = "College" < "Collage"; document.write(z) |
|
||
<= |
Less than or Equal to |
z = "College" <= "Collage"; document.write(z) |
|
These comparisons result from the fact that 'e' comes after 'a' in Latin-1 code.
Remember also that capital letters come before lower-case letters.
<script language="JavaScript1.2"> document.writeln(z) document.writeln(z) |
|
JavaScript 1.3 Strict Equality Operators |
||
Operator |
Definition |
Examples |
=== |
Strictly Equal |
z = "1.0" === 1; document.write(z) |
|
||
!== |
Strictly Not Equal |
z = "1.0" !== 1; document.write(z) |
|
With so many operators around, they must take turns!
z = 1 + 2 * 3 |
|
Some operators, such as addition and subtraction, have the same precedence, in which case they have an association that specifies their order of execution, left-to-right or right-to-left:
z = 3 + 1 - 2 // left-to-right: -> 4 - 2 = 2 |
|
Because of the mathematics and judicious choice of precedence, this usually won't make a difference.
But watch out when concatenating strings and numbers!
z = "Committee of " + 2 + 3 |
|
Occasionally, also, unrelated operators have the same precedence, with interesting results:
z = -~1 // right-to-left: 1 = 0b0...01 -> 0b1...10 = -2 -> 2 |
|
The JavaScript operators you have seen so far, along with a few still to come, are summarized in the following table with their order of precedence and their associativities:
JavaScript Operator Precedences and Associativities |
|||
Operators |
Operator Meaning |
Precedence |
Associativity |
() |
1 |
Left to right |
|
! |
boolean not |
2 |
Right to left |
* / % |
3 |
Left to right |
|
+ - |
4 |
Left to right |
|
<< >> |
5 |
Left to right |
|
< <= > >= |
6 |
Left to right |
|
== === != !== |
7 |
Left to right |
|
& |
8 |
Left to right |
|
^ |
9 |
Left to right |
|
| |
10 |
Left to right |
|
&& |
11 |
Left to right |
|
|| |
12 |
Left to right |
|
?: |
13 |
Right to left |
|
= |
assignment |
14 |
Right to left |
, |
15 |
Left to right |
To work with JavaScript, no special tools are required, just a text editor to write scripts into a .html file (the source view of Dreamweaver can also be used), and a web browser to view the result.
this.value
, and calculate the year when you will be that age. Use the method window.alert('string')
to display the result.