Javascript operators are divided into following groups.
unary operators | which require a single operand |
binary operators | which require two operand |
Trinary operators | This is conditional operator. |
Unary operators can function with types of notation.
- | Negation |
~ | One's complement |
+ | Increment |
-- | Decrement |
! | Logical NOT |
There are different types of binary operator.
In order to assign a value to a variable, you must use he equals sign(=). In addition, javascript provides other assignment operators that allow you to carry out arithmetic logical and bitwise operations along with assignment operations that use a single operator.
These operator allow you to make logical comparisons between operands. The operands that you use with comparison operators can be numeric values or character strings. Javascript compares strings according to their Unicode values.
These operators behave differently according to the data types of your operands.
<, >, <=, >= Operatorsvar myVar1="3", myVar2=3; myVar1==myVar2; //returns 'true' myVar1===myVar2; //returns 'false'
Javascript provides the classical mathematical calculation operators. You can use these operators with integers and with floating point numbers.
Bitwise operators process binary representations of integers that are in either decimal or hexadecimal or octal format. They convert their results into numeric form before they return them.
Logical operators allow you to carry out operations on Boolean expressions so as to evaluate conditions.
It requires three operands. Based on the value of the condition (1st operand) this operator will either run or return one of the other two operands.
condition ? val1 ? val2
This operator is the equivalent of a simplified version of if ... else
delete
operatorTo delete an implicit variable (a variable that you didn't declare using the var keyword), an object the property of an object or an element in an array.
delete implicitVar delete objectVar delete objectVar.property delete array[index]
If the operator is able to carry out the deletion then it returns true. Otherwise it returns false.
If the operator is able to carry out the deletion then the variable that references the object or the element of the array will contain the undefined value.
instanceof
operator This operator returns a boolean value that indicates whether or not an object is an instance of a specific class.
objectVar instance of objectClass
new
operatorYou can use this operator in order to create a new object (instance)
new constructor([arguement1]..,[..,arguementN])
The new operator carries out the following tasks
myObject = new Object(); myArray = new Array(5); myDate = new Date(June 17, 200");
This operator returns a character string that identifies the data type of an expression .
typeof [(] objectClass [)]
the brackets () are optional.
typeof
operatorThis operator can return one of six values: number, Boolean, string, object, function or undefined.
void
operatorThe void operator allows you to evaluate or to execute an expression that does not return a value.
void [(] expression [)]
the brackets () are optional.
This example illustrates the use of the void operator in order to send an HTML form by clicicking a hyperlink.
<a href="javascript: void(document.form.submit())">Click Here</a>
this
operatorThe this
operator allows you to reference the current object in a script so that you can access its properties and its methods.
this.property this.method()
In this context of a browser, this references the objec that represents the HTML object.
Comma Operator allows multiple expressions to be evaluated as single statement typically var and for.
This example shows three comma scenarios. The first scenario is not useful, but the latter two are.
function p(stuff) { print("<<<" + stuff + ">>>"); } p(0, -1, 1); // prints 1 var n = 0, m = -1, o = 1; p(n); p(m); p(o); // prints 0 -1 1 for(var i = 0, j = 1; i < 5; i++, j++) { p("i = " + i + " and j = " + j); } // prints i = 0-4 and j = 1-5
In Operator checks if object has the given property
checks what is returned in a generator by the generator's iterator.