JavaScript Operators, JavaScript Tutorial, Global Guide Line Technology.
Forum | Blog |

      Home                    

Basic JavaScript Guide.
 JScript Introduction.            
 JScript Why?                       
 JScript Recognitions.          
 JScript onMouseover.         
 JScript with Buttons.           
 JScript Alerts.                     
 JScript Variables.               
 JScript Operators.
 JScript Functions.               
 JScript Prompts.                 
 JScript Conditions.             
 JScript Validations.            
 JScript Confirmation Boxes.
 JScript Browser Detection.  
 JScript Redirection.            
 JScript on Links.                 
 JScript Switch.                    
 JScript Loops.                     
 JScript Events.                    
 JScript Exception Handling.
 JScript Reserve Characters. 
 JScript with Objects.            
 JScript Math Object.           
 JScript using Dates.             
 JScript using String.            
 JScript using Arrays.            
 JScript Operations.              
 JScript Handling Cookies.   
 JScript in Mapping.             
 JScript Time Out.                
 JScript Summary.                

JavaScript Tutorial >> JavaScript Operators.


     Back             Next     
Now we will learn in JavaScript Tutorial that Operators in JavaScript allow us to control our values, such as multiplying two numbers, adding two strings together and much more....
JavaScript has a full range of operators, including arithmetic operators, logical operators, bitwise JavaScript Operators, and assignment operators. There are also a few diverse operators in JavaScript that will be explain under here.

Arithmetic Operators In JavaScript

JavaScript Operator Description of JavaScript Operator Examples of JavaScript Operator
+ (add) Adds numeric operands or concatenates two or more strings in JavaScript. 1) v+y

2) "Me"+" You"
Result: "Me You"

- (subtract) Subtracts numeric operands in JavaScript. x-7
* (multiply) Multiplies numeric operands in JavaScript. 5*2
/ (divide) Divides numeric operands. y/v
% (modulo) Returns the remainder when the first operand is divided by the second operand. 7%2
Result: 1
++ (increment) Increments the operand by one. Note the following two possible behaviors in JavaScript increment operator

1) If operator is used before operand like (++v), then it increments the operand and evaluates to the incremented value.

2) If operator is used following operand like (v++), it increments the operand but evaluates to the un incremented value.

Using v=2 for each example below:

1) v++
Result: v=3

2) y= ++v
Result: 3

3) y= v++
Result: y=2

-- decrement Decrements the operand by 1. Note the following two possible behaviors:

1) If operator is used before operand like (--v), it increments the operand and evaluates to the incremented value.

2) If operator is used following operand like (v--), it increments the operand but evaluates to the un incremented value.

v--
Tip: With all of the JavaScript Operators above except Addition +, if an operand used is non numeric, operator will attempt to convert it to a number first.

Comparison Operators in JavaScript

JavaScript Operator Description of Comparison JavaScript Operators Examples of Comparison Operator
== Tests for equality in value between two operands. 3==4
Result: returns false
=== Tests for equality between two operands both in terms of value and type. Supported in JavaScript 1.3+ Using v=3 and y="3":

1) v==y
Result: returns true

2) v===y
Result: returns false

< Less than. v<=y
<= Less than or equal to. 5<=v
> Greater than. y>4
>= Greater than or equal. v>=y

Logical Operators in JavaScript

Logical Operator in JavaScript Description of Logical Operator in JavaScript Examples of Logical Operator
&& (AND) Logical AND. (v<3 && y<=5)
|| (OR) Logical OR (v<3 || y<=5)
! (NOT) Logical NOT. !(v>3)

Bitwise Operators in JavaScript

Bitwise Operator Description of Bitwise Operator
& Bitwise AND.
| Bitwise OR
^ Bitwise XOR.
<< Shift left.
>> Shift right.
>>> Shift right, zero fill.

Other Operators in JavaScript

Other Operator Description of Other Operators in JavaScript
?: "?:", or the Conditional Operator in JavaScript, is a shorthand method for constructing an evaluation, and executing two different assignments depending on the result. The syntax is:

{condition)? iftrue : iffalse

For example:

y=-1
v=(y<0)? 5: 10

v will contain "5" in the above case.


typeof The "typeof" operator in JavaScript allows us to probe the data type of its operand, such as whether a JavaScript variable is string, numeric, or even undefined. Here are some of the possible return values:
Evaluates to Indicates
"number" Operand is a number
"string" Operand is a string
"boolean" Operand is a Boolean
"object" Operand is an object
null Operand is null.
"undefined" Operand is not defined.
instanceof Returns a Boolean value indicating whether the left hand operand is of the type specified in the right hand operand. For example:

var today=new Date()
alert(today instanceof Number)
//alerts false, since today is not a Number.

delete Deletes a custom object property, method, or array element. For example:

var mycar=new Object()
mycar.color="red"
delete mycar.color //deletes the property "color"


Bitwise JavaScript Operators Example

This example uses the Bitwise Operator Shift Left to shift the value 2 to the left four bits (binary 10 to binary 1000), resulting in 8:

var test=2;
test=test<<2
alert(test) //alerts 8

Conditional JavaScript Operators Example

The Conditional Operator is extremely handy for quickly assigning a different value to a variable depending on the outcome of an evaluation. Here are two examples:

var browser=(navigator.appName.indexOf("Microsoft")!=-1)? "IE" : "Non IE"

You can expand the number of possible values to assign to more than just two. In fact, there is no limit. Observe the below example, which has 3 possible values:

var browser=(navigator.appName.indexOf("Microsoft")!=-1)? "IE" : (navigator.appName.indexOf("Netscape")? "NS" : "Not IE nor NS"

In the 2nd example here in JavaScript Tutorial, "browser" will contain one of 3 possible values depending on the browser maker of the visitor.

     Back             Next     
 

JavaScript Operators Examples.



[ About ] [ Contact ] [ Home ]
[ Links ] [ Site Map ] [ Services ] [ Privacy ]

Copyright © 2005 -  2023 www.globalguideline.com All rights reserved. (Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher) 357 visitors are online now