Operators

Javascript operators are divided into following groups.

unary operatorswhich require a single operand
binary operatorswhich require two operand
Trinary operatorsThis is conditional operator.
I. Unary Operators

Unary operators can function with types of notation.

  • prefix notation, with which the operator appears before the operand.
  • postfix notation, with which the operator appears after the operand.
Operator Definition
-Negation
~One's complement
+Increment
--Decrement
!Logical NOT
II. Binary Operators

There are different types of binary operator.

  1. Assignment Operators
  2. Comparison Operators
  3. Arithmetic Operators
  4. Bitwise Operators
  5. Logical Operators
  6. Special Operators
1. Assignment Operators

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.

2. Comparison 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.

<, >, <=, >= Operators
  • - These operators attempt to convert operands into numbers
  • - If both of the operands are strings, these operators compare according to the Unicode values of the characters.
  • - If the value of one of these operands is NaN, then the operator will return false.
==, != Operators
  • - if the operands are of different data types, then these operators will attempt to convert the operands either into strings or into numbers or into Booleans.
  • - These operators consider null to be equal to null and also to be equal to undefined.
===, !=== Operators
  • - These operators act in the same way as the == and the != operators exceps that they do nbt convert the data types of the two operands. these operators compare according to the values of the operands and according to the data types of the operands.
var myVar1="3", myVar2=3;
myVar1==myVar2; //returns 'true'
myVar1===myVar2; //returns 'false'
3. Arithmetic Operators

Javascript provides the classical mathematical calculation operators. You can use these operators with integers and with floating point numbers.

Note
  • If you divide by 0, then javascript will return the Infinity value. If you modulus by 0 then javascript will return the NaN(Not a number) value.
  • You can also use the addition operator (+) in order to concatenate character strings.
4. Bitwise Operator

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.

  • The binary AND operator will return 1(true) only if both of the operands are 1(true)
  • The binary OR operator will return 1(true) if atleast one of the two operands is 1(true)
  • The binary Exclusive OR operator will return 1(true) only if only one of the two operands is 1(true)
5. Logical Operators

Logical operators allow you to carry out operations on Boolean expressions so as to evaluate conditions.

6. Special Operators
Conditional (trinary) operators -

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.

Syntax
condition ? val1 ? val2

This operator is the equivalent of a simplified version of if ... else

delete operator

To 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.

Syntax
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.

Syntax
objectVar instance of objectClass
new operator

You can use this operator in order to create a new object (instance)

Syntax
new constructor([arguement1]..,[..,arguementN])

The new operator carries out the following tasks

  • It creates an object without a member
  • It calls the constructor of this object. The constructor then initializes the Object according to its arguments. The operator then returns a pointer to the Object.
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 .

Syntax
typeof [(] objectClass [)]

the brackets () are optional.

typeof operator

This operator can return one of six values: number, Boolean, string, object, function or undefined.

void operator

The void operator allows you to evaluate or to execute an expression that does not return a value.

Syntax
void [(] expression [)]

the brackets () are optional.

Example.

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 operator

The this operator allows you to reference the current object in a script so that you can access its properties and its methods.

Syntax:
this.property
this.method()

In this context of a browser, this references the objec that represents the HTML object.

comma operator

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.

Example
snippet
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

In Operator checks if object has the given property

yield operator

checks what is returned in a generator by the generator's iterator.

Order of priority for operator
todo..
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +