Wednesday 17 June 2020

The Arithmetic,Comparison, Logical and String Operators in JavaScript. Operator precedence.

The Arithmetic,Comparison, Logical
and String Operators in JavaScript.
Operator precedence.

Variables में values सिर्फ उन्हें बाद में display करवाने के लिए ही नहीं बल्कि इसलिए भी store की जाती है  ताकि उनके साथ अलग अलग तरह के operations perform किये जा सके। Variables की values के साथ operations perform करने के लिए आपको operators की जरुरत होती है।


Types of Operators


Arithmetic Operators

Arithmetic operations perform करने के लिए arithmetic operators यूज़ किये जाते है।

Operator

Explanation

Example

Addition (+)

It adds values of 2 or more variables

a+b

Subtraction(-)

Subtract the value of one variable from other variables value

a-b

Multiplication(*)

Multiply values of 2 variables

a*b

Division(/)

Divide the value of one variable by the value of another variable.

a/b

Modulus

Get the remainder after division

a%b

Exponentiation

Value of first variable raise to power to value of second variable

a**b


Addition(+) Arithmetic Operator

Operands को add करने के लिए Addition Arithmetic Operator का इस्तेमाल किया जाता है |

Source Code :

<script type="text/javascript">

 

var a = 6, b = 3, c;

 

c = a + b;

document.write("Value of c is " + c);

 

</script>

Output :

Value of c is 9



Subtraction(-) Arithmetic Operator

Operands को subtract करने के लिए Subtraction Arithmetic Operator का इस्तेमाल किया जाता है |

Source Code :

<script type="text/javascript">

 

var a = 6, b = 3, c;

 

c = a - b;

document.write("Value of c is " + c);

 

</script>

Output :

Value of c is 3



Multiplication(*) Arithmetic Operator

Operands को multiply करने के लिए Multiplication Arithmetic Operator का इस्तेमाल किया जाता है |

Source Code :

<script type="text/javascript">

 

var a = 6, b = 3, c;

 

c = a * b;

document.write("Value of c is " + c);

 

</script>

Output :

Value of c is 18



Division(/) Arithmetic Operator

Operands को divide करने के लिए Division Arithmetic Operator का इस्तेमाल किया जाता है |

Source Code :

<script type="text/javascript">

 

var a = 6, b = 3, c;

 

c = a / b;

document.write("Value of c is " + c);

 

</script>

Output :

Value of c is 2



Modulus(%) Arithmetic Operator

दो Operands से remainder निकालने के लिए Modulus Arithmetic Operator का इस्तेमाल किया जाता है |

Source Code :

<script type="text/javascript">

 

var a = 6, b = 3, c;

 

c = a % b;

document.write("Value of c is " + c);

 

</script>

Output :

Value of c is 0



Increment(++) Arithmetic Operator

Operand को एक से बढ़ाने के लिए Increment Arithmetic Operator का इस्तेमाल किया जाता है |

Example 1 :

Source Code :

<script type="text/javascript">

var a = 6;

b = ++a; //prefix increment

document.write("Value of b is " + b + "<br />");

document.write("Value of a is " + a + "<br />");

 

</script>

Output :

Value of b is 7

Value of a is 7



Example 2 :

Source Code :

<script type="text/javascript">

 

var a = 6;

b = a++; //postfix increment

document.write("Value of b is " + b + "<br />");

document.write("Value of a is " + a + "<br />");

</script>

Output :

Value of b is 6

Value of a is 7



Decrement(--) Arithmetic Operator

Operand को एक से घटने के लिए Decrement Arithmetic Operator का इस्तेमाल किया जाता है |

Example 1 :

Source Code :

<script type="text/javascript">

 

var a = 6;

b = --a; //prefix decrement

document.write("Value of b is " + b + "<br />");

document.write("Value of a is " + a + "<br />");

 

</script>

Output :

Value of b is 5

Value of a is 5



Example 2 :

Source Code :

<script type="text/javascript">

 

var a = 6;

b = a--; //postfix decrement

document.write("Value of b is " + b + "<br />");

document.write("Value of a is " + a + "<br />");

</script>

Output :

Value of b is 6

Value of a is 5

 

                                                Logical Operators 

Logical operators के द्वारा logic perform किया जाता है। इन operators को control statements में यूज़ किया जाता है।

Operator

Explanation

Example

And(&&)

यदि दोनों variables की value true है तो ये operator true result return करता है।

a&&b

Or(||)

दोनों में से कोई एक variable true हो तो भी result true ही होता है।

a||b

Not(!)

यदि variable true है तो false होगा और यदि false है तो true हो जायेगा।

!a

Xor

यदि दोनों से कोई एक true है तो result true होगा। और यदि दोनों false या दोनों true है तो result false होगा।

a Xor b


Logical AND(&&) Logical Operator

जब दिए हुए सभी expression 'true' होते है तो ये 'true' return करता है | अगर हुए expressions से एक expression अगर false हुई तो ये false return करता है |

Source Code :

<script type="text/javascript">

 

var a = 10, b = 15;

 

var x = (a < b) && (b > a);

 

document.write(x);

 

</script>

Output :

true



Logical OR(||) Logical Operator

Expressions में से एक expression अगर 'true' होता है तो ये 'true' return करता है |

Source Code :

<script type="text/javascript">

 

var a = 10, b = 15;

 

var x = (a < b) || (b < a);

 

document.write(x);

 

</script>

Output :

true



Logical NOT(!) Logical Operator

Logical NOT Operator जब expression false होता है तब 'true' return करता है और अगर expression true होता है तो 'false' return करता है |

Source Code :

<script type="text/javascript">

 

var a = 10, b = 15;

 

var x = (a < b) || (b < a) ;

document.write(x + "<br />");

 

var x = !(a < b) || (b < a) ;

document.write(x);

 

</script>

Output :

true

false

 

Assignment Operators 

Assignment operators variables की values आपस में assign करने के लिए यूज़ किये जाते है।

Operator

Explanation

Example

Simple assignment (=)

ये operator right variable की value left variable को assign करता है।

a=b;

Plus assignment (+=)

ये operator left और right variables की value को add करके left variable में store करता है।

a+=b;

Minus assignment(-=)

ये operator left side के variable की value में से right side के variable की value घटाकर result left side के variable में store करता है।

a-=b

Multiply assignment(*=)

ये operator left और right side के variables की values को multiply करके result left side के variable में store करता है।

a*=b

Divide assignment(/=)

ये operator left side के variable की value को right side के variable से divide करके result left side के variable में store करता है।

a/=b

 

 

                                  Example

 

<!DOCTYPE html>

<html>

<body>

 

<p>b = 5, calculate a = b + 2, and display a:</p>

 

<button onclick="myFunction()">Try it</button>

 

<p id="demo"></p>

 

<script>

function myFunction() {

    var b = 5;

    var a = b + 2;

    document.getElementById("demo").innerHTML = a;

}

</script>

 

</body>

</html>

Result

b = 5, calculate a = b + 2, and display a:

Assignment(=) Operator

Source Code :

<script type="text/javascript">

 

var a = 10;

var b = a;

document.write("Value of b is " + b);

 

</script>

Output :

Value of b is 10



Addition Assignment(+=) Operator

Source Code :

<script type="text/javascript">

 

var a = 10, b = 15;

a += b;

document.write("Value of a is " + a);

 

</script>

Output :

Value of a is 25



Subtraction Assignment(-=) Operator

Source Code :

<script type="text/javascript">

 

var a = 10, b = 15;

a -= b;

document.write("Value of a is " + a);

 

</script>

Output :

Value of a is -5



MultiplicationAssignment(*=) Operator

Source Code :

<script type="text/javascript">

 

var a = 10, b = 15;

a *= b;

document.write("Value of a is " + a);

 

</script>

Output :

Value of a is 150



Division Assignment(/=) Operator

Source Code :

<script type="text/javascript">

 

var a = 10, b = 15;

a /= b;

document.write("Value of a is " + a);

 

</script>

Output :

Value of a is 0.6666666666666666



Modulus Assignment(%=) Operator

Source Code :

<script type="text/javascript">

 

var a = 10, b = 15;

a %= b;

document.write("Value of a is " + a);

 

</script>

Output :

Value of a is 10



Bitwise AND Assignment(&=) Operator

Source Code :

<script type="text/javascript">

 

var a = 10, b = 15;

a &= b;

document.write("Value of a is " + a);

 

</script>

Output :

Value of a is 10



Bitwise OR Assignment(|=) Operator

Source Code :

<script type="text/javascript">

 

var a = 10, b = 15;

a |= b;

document.write("Value of a is " + a);

 

</script>

Output :

Value of a is 15



Bitwise Exclusive OR Assignment(^=) Operator

Source Code :

<script type="text/javascript">

 

var a = 10, b = 15;

a ^= b;

document.write("Value of a is " + a);

 

</script>

Output :

Value of a is 5



Left Shift Assignment(<<=) Operator

Source Code :

<script type="text/javascript">

 

var a = 10, b = 15;

a <<= b;

document.write("Value of a is " + a);

 

</script>

Output :

Value of a is 327680



Right Shift Assignment(>>=) Operator

Source Code :

<script type="text/javascript">

 

var a = 10, b = 15;

a >>= b;

document.write("Value of a is " + a);

 

</script>

Output :

Value of a is 0

 

Comparison Operators

Comparison Operators दो operands की condition boolean value में return करता है |

Operators

Description

Examples

==

Equal to

if a = 6, b = "6"
return true

===

Identical Equal to

if a = 6, b = "6"
return false

< 

Less than

if a = 6, b = 5
a < b
return false

<=

Less than or Equal to

if a = 6, b = 5
a <= b
return false

> 

Greater than

if a = 6, b = 5
a > b
return true

>=

Greater than or Equal to

if a = 6, b = 5
a >= b
return true

!=

Not Equal to

if a = 6, b = "6"
a != b
return false

!==

Idential Not Equal to

if a = 6, b = "6"
a !== b
return true

 

'Equal to'(==) Comparison Operator

Equal to Comparison Operator; operands equal है या नहीं ये check करता है |

Source Code :

true

Output :

true



'Identical Not Equal to'(===) Comparison Operator

Identical Equal to Comparison Operator; operands के data types और उनकी values को check करता है |

Source Code :

<script type="text/javascript">

 

var a = 6, b = "6";

document.write(a===b);

 

</script>

Output :

false



'Less than'(<) Comparison Operator

Less than Comparison Operator; ये left operand; right operand से छोटा है या नहीं ये check करता है |

Source Code :

<script type="text/javascript">

 

var a = 6, b = 5;

document.write(a<b);

 

</script>

Output :

false

'Less than or Equal to'(<=) Comparison Operator Less than or Equal to Comparison Operator; ये left operand; right operand से छोटा या बराबर है या नहीं ये check करता है |

Source Code :

<script type="text/javascript">

 

var a = 6, b = 5;

document.write(a<=b);

 

</script>

Output :

false



'Greater than'(>) Comparison Operator

Greater than Comparison Operator; ये left operand; right operand से बड़ा है या नहीं ये check करता है |

Source Code :

<script type="text/javascript">

 

var a = 6, b = 5;

document.write(a>b);

 

</script>

Output :

true



'Greater than or Equal to'(>=) Comparison Operator

Greater than or Equal to Comparison Operator; ये left operand; right operand से बड़ा या बराबर है या नहीं ये check करता है |

Source Code :

<script type="text/javascript">

 

var a = 6, b = 5;

document.write(a>=b);

 

</script>

Output :

true



'Not Equal to'(!=) Comparison Operator

Not Equal to Comparison Operator; operands equal है या नहीं ये check करता है |

Source Code :

<script type="text/javascript">

 

var a = 6, b = 5;

document.write(a!=b);

 

</script>

Output :

true



'Identical Not Equal to'(!==) Comparison Operator

Identical Not Equal to Comparison Operator; operands का data types और उनकी values equal है या नहीं ये check करता है |

Source Code :

<script type="text/javascript">

 

var a = 6, b = "6";

document.write(a!=b);

 

var c = 6, d = "6";

document.write(c!==d);

 

</script>

Output :

falsetrue

String Introduction

String ये Characters का sequence होता है |

String को single या double quotes के अन्दर लिखा जाता है |

var str1 = 'Hello';

var str2 = "World";



Empty String

var str3 = "";



Strings को दो प्रकार से create किये जाते है |

·         String Type

·         String Object Type

var str4 = "Hello World"; //String Type

var str5 = new String("Hello World"); //String Object Type



Check Strings Equal or Not using '==' Operator

'==' Operator से दोनों Strings equal है |

Source Code :

<script type="text/javascript">

 

var str4 = new String("Hello World"); //String Object Type

var str5 = "Hello World"; //String Type

 

document.write(str4 == str5);

 

</script>

Output :

true



Check Strings Equal or Not using '==' Operator

'===' Operator से दोनों Strings equal नहीं है, क्योंकि उनके data types अलग-अलग है |

Source Code :

<script type="text/javascript">

 

var str4 = new String("Hello World"); //String Object Type

var str5 = "Hello World"; //String Type

 

document.write(str4 === str5);

 

</script>

Output :

false



निचे दिए हुए Example में देखे तो double quoted String में double quoted String दिया हुआ है | इससे String access नहीं होता |

var str = "Hello Friends! My name is "Rakesh"";

       document.write(str);



यदि double quoted string के अन्दर single quoted और single quoted string के अन्दर double quoted string हो तो वो access हो जाता है |

Source Code :

<script type="text/javascript">

 

var str1 = "Hello Friends! My name is 'Rakesh'";

       document.write(str1 + "
");

var str2 = 'Hello Friends! My name is "Rakesh"';

       document.write(str2 + "
");

 

</script>

Output :

Hello Friends! My name is 'Rakesh'

Hello Friends! My name is "Rakesh"



अगर single quoted string के अन्दर single quoted string और double quoted string के अन्दर double quoted strig को access करने के लिए पहले escape character (\) का इस्तेमाल करना पड़ता है |

अगर String में special characters को executes करना हो तो special characters से पहले escape character(\) का इस्तेमाल करना पड़ता है |

Source Code :

<script type="text/javascript">

 

var str1 = "Hello Friends! My name is \"Rakesh\"";

       document.write(str1 + "
");

var str2 = 'Hello Friends! My name is \'Rakesh\'';

       document.write(str2 + "
");

 

</script>

Output :

Hello Friends! My name is "Rakesh"

Hello Friends! My name is 'Rakesh'



String Length Property

String की length ये बहुत ही महत्वपूर्ण property है |

Source Code :

<script type="text/javascript">

 

var str = "Hello World";

       document.write(str.length);

      

</script>

Output :

11

 

JavaScript Operator Precedence Values

Pale red entries indicates ECMAScript 2015 (ES6) or higher.

Value

Operator

Description

Example

20

( )

Expression grouping

(3 + 4)

 

 

 

 

19

.

Member

person.name

19

[]

Member

person["name"]

19

()

Function call

myFunction()

19

new

Create

new Date()

 

 

 

 

17

++

Postfix Increment

i++

17

--

Postfix Decrement

i--

 

 

 

 

16

++

Prefix Increment

++i

16

--

Prefix Decrement

--i

16

!

Logical not

!(x==y)

16

typeof

Type

typeof x

 

 

 

 

15

**

Exponentiation (ES2016)

10 ** 2

 

 

 

 

14

*

Multiplication

10 * 5

14

/

Division

10 / 5

14

%

Division Remainder

10 % 5

 

 

 

 

13

+

Addition

10 + 5

13

-

Subtraction

10 - 5

 

 

 

 

12

<< 

Shift left

x << 2

12

>> 

Shift right

x >> 2

12

>>> 

Shift right (unsigned)

x >>> 2

 

 

 

 

11

< 

Less than

x < y 

11

<=

Less than or equal

x <= y

11

> 

Greater than

x > y

11

>=

Greater than or equal

x >= y

11

in

Property in Object

"PI" in Math

11

instanceof

Instance of Object

instanceof Array

 

 

 

 

10

==

Equal

x == y

10

===

Strict equal

x === y

10

!=

Unequal

x != y

10

!==

Strict unequal

x !== y

 

 

 

 

9

&

Bitwise AND

x & y

8

^

Bitwise XOR

x ^ y

7

|

Bitwise OR

x | y

6

&&

Logical AND

x && y

5

||

Logical OR

x || y

4

? :

Condition

? "Yes" : "No"

 

 

 

 

3

+=

Assignment

x += y

3

/=

Assignment

x /= y

3

-=

Assignment

x -= y

3

*=

Assignment

x *= y

3

%=

Assignment

x %= y

3

<<=

Assignment

x <<= y

3

>>=

Assignment

x >>= y

3

>>>=

Assignment

x >>>= y

3

&=

Assignment

x &= y

3

^=

Assignment

x ^= y

3

|=

Assignment

x |= y

 

 

 

 

2

yield

Pause Function

yield x

1

,

Comma

5 , 6

 


No comments:

Post a Comment

Please Do Not Enter Any Spam Link in the comment Box.