JavaScript Operators are the symbols that represent operations that can be carried out on values in script. An operator can be used with one or more operands to do an operation. Operations can be mathematical, text, logical or simply transformation of data.
The operands can be-
- variables
- literals like integer, string or float values
- expression- made up of variables and operators
JavaScript Operators- Types
- Unary- these operators require single operand (example- increment, decrement, one’s complement, negation)
- Binary- These operators require two operands for its operation (example- addition, division)
- Ternary- These operators require three operands (conditional operator)
JavaScript Operators-Categories
Arithmetic Operators
Mathematical operations are performed using Arithmetic Operators. Binary Arithmetic operators are Addition (+), Subtraction (-), Multiplication (*), Division (/) and Remainder or Modular (%). Unary arithmetic operators are Increment (++) or Decrement (–).
<BODY> <SCRIPT LANGUAGE="JavaScript"> num1=20; num2=5; str1="Hello"; str2="All"; document.write("<BR>num1+num2=",num1+num2); document.write("<BR>num1-num2=",num1-num2); document.write("<BR>num1*num2=",num1*num2); document.write("<BR>num1/num2=",num1/num2); document.write("<BR>num1%num2=",num1%num2); document.write("<BR>Increment=",++num1); document.write("<BR>Decrement=",--num2); </SCRIPT> </BODY>
Comparison Operators
Comparison operators are used to compare different values and return Boolean values depending upon whether the comparison is true or false. All comparison operators are binary. The JavaScript Operators in this category are – Greater Than (>), Less Than (<), Greater Than or Equal To (>=), Less Than or Equal To (<), Not Equal To (!=, !==), Equal To (==, ===).
<BODY> <SCRIPT LANGUAGE="JavaScript"> num1=20; num2=5; document.write("<BR>num1>num2=",num1>num2); document.write("<BR>num1<num2=",num1<num2); document.write("<BR>num1>=num2=",num1>=num2); document.write("<BR>num1<=num2=",num1<=num2); document.write("<BR>num1==num2=",num1==num2); document.write("<BR>num1!=num2=",num1!=num2); </SCRIPT> </BODY>
Logical Operators
Boolean Operators are used to combine two or more Logical expressions and evaluate truth value of combined logical expressions. The JavaScript operators for logical evaluation are AND (&&), OR (II) and NOT (!).
<BODY> <SCRIPT LANGUAGE="JavaScript"> num1=20; num2=5; document.write("<BR>(5<6)&&(30<40)=",(5<6)&&(30<40)); document.write("<BR>(5>6)||(30<40)=",(5>6)||(30<40)); document.write("<BR>!(5>6)=",(5>6)); </SCRIPT> </BODY>
Bitwise Operators
Bitwise operators work on the binary value of the operands and evaluate by shifting bits or performing binary arithmetic like AND (&), OR(|), XOR(^), One’s Complement(~), Left Shift(<<), Right Shift(>>) and Right Shift with 0 fill (>>>).
Assignment Operators
Assignment operators assign the value given in the right hand side to the left hand side variable. The Assignment Operator (=) can be combined with arithmetic operators to perform the operation between left and right operands and assign the evaluated value to the left variable. This is the shorter way to write a statement that involves arithmetic or bitwise operations. These operators are +=, -=, *=, /=, %= and bitwise assignment operators (&=, !=, ^=,<<=, >>=).
<BODY> <SCRIPT LANGUAGE="JavaScript"> num1=20; num2=5; document.write("<BR>num1+=2->",num1+=2); document.write("<BR>num1-=2->",num1-=2); document.write("<BR>num1*=2->",num1*=2); document.write("<BR>num1/=2->",num1/=2); document.write("<BR>num1%=2->",num1%=2); document.write("<BR>num1&=2->",num1&=2); document.write("<BR>num1|=2->",num1|=2); </SCRIPT> </BODY>
String Operator
The string operator is the overloaded + operator which concatenates if the operands are strings. It can be used between more than one string variables or literals to create single string.
<BODY> <SCRIPT LANGUAGE="JavaScript"> str1="Hello"; str2="All"; document.write("<BR>"+str1+str2+"<BR><BR>"); </SCRIPT> </BODY>
Conditional Operator
This is the ternary operator which can be used in place of an if-else statement. The first part is the condition using comparison operators or a logical statement using logical and comparison operators. Second part represents statement to be executed if first part condition is true. Third part represents statement to be executed if the first condition is false. The Syntax is-
Condition Statement? Expression to be evaluated when condition is true: Expression to be evaluated when condition is false;
<BODY> <SCRIPT LANGUAGE="JavaScript"> num1=20; num2=5; document.write(num1>=num2? num1+10:num1+20); </SCRIPT> </BODY>
Typeof Operator
The typeof operator returns the data type of the variable, expression or value given in the parenthesis.
<BODY> <SCRIPT LANGUAGE="JavaScript"> num1=20; str1="Hello"; document.write("<BR>"+typeof(num1)+","+typeof(str1)); </SCRIPT> </BODY>