3. Operator in Javascript

Operators in JavaScript

- Operators in JavaScript are very similar to the operators that appear in the other programming languages, such as C++, or Java.

- Operators are used extensively in JavaScript program to perform some sort of calculation, comparison, or assignment on one or more values.



Operator Sub-types in JavaScript

There are three sub-types of operators in JavaScript. They are:

1. Unary : Unary operator is an operator that takes only a single operand.

2. Binary : Binary operator is an operator that takes two operands.

3. Ternary : Ternary operator is an operator that takes three operands.





Precedence of Arithmetic Operators in JavaScript

There are two priority levels of arithmetic operation in JavaScript. They are as follows:

High priority: * / %

Low priority: + –

If two or more sets of parentheses occur into the expression one after another, the order of evaluation will be done from the left set towards the right set.


Types of Operators in JavaScript

In JavaScript programming language, operators are classified into eight different categories:


Arithmetic Operators

Assignment Operators

Comparison (Relational) Operators

Logical Operators

Bitwise Operators

Conditional Operators (Ternary )



1. Arithmetic (Mathematical) Operators in JavaScript

Arithmetic operators in JavaScript are used to perform the most common mathematical calculations or arithmetic operations on the operands, or two or more values.

- Addition (+)

- Subtraction (-)

- Multiplication(*)

- Division (-)

- Modulus (Remainder)(%)


2. Assignment Operator

- An operator that is used to assign/update the value of a variable is called assignment operator in JavaScript.

- The most common assignment operator is equal operator “=”, which assigns the right-hand value to the left-hand variable.


Types of Assignment Operator in JavaScript

1. Simple assignment 

- Ex : var x = 20;

 

2. Compound assignment (ShortHand)

- Ex : x += 10;


3. Assignment as expression

-Ex : int a = b – c + 4;



3. Comparison Operators

- Comparison operators in JavaScript are those operators that are used for comparing between two numeric values, strings, objects, or two quantities.

Types of Comparison Operators in JavaScript

JavaScript supports eight types of relational or comparison operator

1.  == (Equal) : This operator evaluates that the value of left and right operands are equal or not. If values on both sides of the operator are equal to each other, the equal operator (==) returns true.


2. != (Not Equal)

3. > (Greater than)

4. < (Less than)

5. >= (Greater than or Equal to)

6. <= (Less than or Equal to)

7. === (Strictly Equal or Identical) : This operator strictly checks whether the left and right values are equal and of the same data type. If the values on both sides are equal and of the same data type, the strict is equal to (===) operator returns true.


8. !== (Strictly not Equal or not Identical)



4. Logical Operators

- Logical operators in JavaScript are those operators which are used to form compound conditions by combining two or more simple conditions or relations.

- Types of Boolean Operators in JavaScript

1. && Logical AND

2. || Logical OR

3. ! Logical NOT 



5. Conditional Operator

- Conditional operator in JavaScript provides a one line approach for creating a simple conditional statement.

- The conditional operator (?:) is also known as ternary operator because it takes three operands and perform a conditional test.

Ex : conditional_expression ? Value1 : Value2

Or,

variable  = conditional_expression ? Value1 : Value2

var a = (x > y) ? 4 : 5 

if condition true output should 4 else 5 .




6. Bitwise Operators 

- Bitwise operators in JavaScript are those operators that works at bit level comprising ones and zeros (i.e., binary representation) rather than decimals or hexadecimals. Here, the term bitwise means to operate on binary numbers.
- JavaScript bitwise operators only work with integer numbers that are 32 bits in length.
- In bitwise operations, the internal representation of integer numbers is represented by the binary number system. A binary number is represented by two digits, 0 or 1.

Types of Bitwise Operators in JavaScript
1. & Bitwise AND binary
2. | Bitwise OR binary
3. ^ Bitwise XOR (Exclusive OR) binary
4. ~ Bitwise NOT unary


1. Bitwise AND Operator (&)
- If the corresponding bits of two operands (i.e., both bits are 1) are equal to 1, the result is 1.

2. Bitwise OR Operator ( | ) in JavaScript
- In OR operation, each bit of the first operand (number) is compared with the corresponding bit of the second operand. If at least one of the compared bits is 1 then it returns 1. Otherwise, it returns 0. 

3.Bitwise XOR operator (^)
- If the odd number of 1’s is in input bits, we get output bit as 1. In other words, if two bits have the same value, the output is 0, otherwise, the output is 1.

4. Bitwise NOT operator (~)
- The bitwise NOT operator is basically an inverter. It returns the reverse of its operand or value. It converts all 1s to 0s, and all of the 0s to 1s. Therefore, it is also called unary operator or bit flip operator.

0 1
1 0


Comments

Popular posts from this blog

1. Introduction of Javascript

2. Datatypes & Variables in Javascript

6. Object and Array in Javascript