Tuesday, July 18, 2023

Javascript Operator

 

OPERATORS

§    Arithmetic Operators 

§    Unary Operators

§    Comparison Operators

§    Logical (or Relational) Operators

§    Assignment Operators

§    Conditional (or ternary) Operators

§    Bitwise Operator

 

Arithmetic Operators 

+ (Addition)

- (Subtraction)

* (Multiplication)

/ (Division)

% (Modulus)

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script type="text/javascript" src="samplejs1.js"></script>

    <title>Document</title>

    <h2 style="height: 40px;width: auto;font-family: Arial, Helvetica, sans-serif;font-size: medium;background-color: orange;color: purple;">Javascript</h2>

</head>

<body>

    

</body>

</html>

Samplejs1.js

var a = 33;

var b = 10;

var c = "Test";

var linebreak = "<br />";

document.write("a + b = ");

result = a + b;

document.write(result);

document.write(linebreak);

document.write("a - b = ");

result = a - b;

document.write(result);

document.write(linebreak);

document.write("a / b = ");

result = a / b;

document.write(result);

document.write(linebreak);

document.write("a % b = ");

result = a % b;

document.write(result);

document.write(linebreak);

 

Output









Arithmetic Operator(Unary Operator)

++ - (increment)- Pre(++a) &Post(a++)

-- (Decrement)-Pre(--a) & Post(a--)

Samplejs1.js

var a = 33;

var b = 10;

var c = "Test";

var linebreak = "<br />";

document.write("a++ = ");

result = a++;

document.write(result);

document.write(linebreak);

document.write("b-- = ");

result = b--;

document.write(result);

document.write(linebreak)

document.write("++a = ");

result = ++a;

document.write(result);

document.write(linebreak);

document.write("--b = ");

result = --b;

document.write(result);

document.write(linebreak)

Output















Comparison Operators

== (Equal)

!= (Not Equal)

> (Greater than)

< (Less than)

>= (Greater than or Equal to)

<= (Less than or Equal to)

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script type="text/javascript" src="samplejs1.js"></script>

    <title>Document</title>

    <h2 style="height: 40px;width: auto;font-family: Arial, Helvetica, sans-serif;font-size: medium;background-color: orange;color: purple;">Javascript</h2>

</head>

<body>

    

</body>

</html>

Samplejs1.js

var a = 33;

var b = 10;

var c = "Test";

var linebreak = "<br />";

document.write("(a == b) => ");

result = (a == b);

document.write(result);

document.write(linebreak);

document.write("(a < b) => ");

result = (a < b);

document.write(result);

document.write(linebreak);

document.write("(a > b) => ");

result = (a > b);

document.write(result);

document.write(linebreak);

document.write("(a != b) => ");

result = (a != b);

document.write(result);

document.write(linebreak);

document.write("(a >= b) => ");

result = (a >= b);

document.write(result);

document.write(linebreak);

document.write("(a <= b) => ");

result = (a <= b);

document.write(result);

document.write(linebreak);

Output








Logical Operators

&& (Logical AND)

|| (Logical OR)

! (Logical NOT)

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    

    <title>Document</title>

    <h2 style="height: 40px;width: auto;font-family: Arial, Helvetica, sans-serif;font-size: medium;background-color: orange;color: purple;">Javascript</h2>

    <script type="text/javascript" src="samplejs1.js"></script>

</head>

<body>

    

</body>

</html>

Samplejs1.js

var a = true;

var b = false;

var linebreak = "<br />";

document.write("(a && b) => ");

result = (a && b);

document.write(result);

document.write(linebreak);

document.write("(a || b) => ");

result = (a || b);

document.write(result);

document.write(linebreak);

document.write("(!a ) => ");

result = (!a);

document.write(result);

document.write(linebreak)

 

Output







Bitwise Operators

& (Bitwise AND)

| (BitWise OR)

^ (Bitwise XOR)

~ (Bitwise Not)

<< (Left Shift)

>> (Right Shift)

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    

    <title>Document</title>

    <h2 style="height: 40px;width: auto;font-family: Arial, Helvetica, sans-serif;font-size: medium;background-color: orange;color: purple;">Javascript</h2>

    <script type="text/javascript" src="samplejs1.js"></script>

</head>

<body>

    

</body>

</html>

 

var a = true;

var b = false;

var linebreak = "<br />";

document.write("(a & b) => ");

result = (a & b);

document.write(result);

document.write(linebreak);

document.write("(a | b) => ");

result = (a | b);

document.write(result);

document.write(linebreak);

document.write("(a ^ b) => ");

result = (a ^ b);

document.write(result);

document.write(linebreak);

document.write("(~b) => ");

result = (~b);

document.write(result);

document.write(linebreak);

document.write("(a << b) => ");

result = (a << b);

document.write(result);

document.write(linebreak);

document.write("(a >> b) => ");

result = (a >> b);

document.write(result);

document.write(linebreak);

 

Output












Assignment Operators

= (Simple Assignment )

+= (Add and Assignment)

-= (Subtract and Assignment)

*= (Multiply and Assignment)

/= (Divide and Assignment)

%= (Modules and Assignment)

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    

    <title>Document</title>

    <h2 style="height: 40px;width: auto;font-family: Arial, Helvetica, sans-serif;font-size: medium;background-color: orange;color: purple;">Javascript</h2>

    <script type="text/javascript" src="samplejs1.js"></script>

</head>

<body>

    

</body>

</html>

 

var a ;

var b = 2;

var linebreak = "<br />";

document.write("Value of a => (a = b) => ");

result = (a = b);

document.write(result);

document.write(linebreak);

document.write("Value of a => (a += b) => ");

result = (a += b);

document.write(result);

document.write(linebreak);

document.write("Value of a => (a -= b) => ");

result = (a -= b);

document.write(result);

document.write(linebreak);

document.write("Value of a => (a *= b) => ");

result = (a *= b);

document.write(result);

document.write(linebreak);

document.write("Value of a => (a /= b) => ");

result = (a /= b);

document.write(result);

document.write(linebreak);

document.write("Value of a => (a %= b) => ");

result = (a %= b);

document.write(result);

document.write(linebreak);

Output












Miscellaneous Operators  (or) Conditional Operator

Syntax

(Condition )? True statement : False  statement;

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    

    <title>Document</title>

    <h2 style="height: 40px;width: auto;font-family: Arial, Helvetica, sans-serif;font-size: medium;background-color: orange;color: purple;">Javascript</h2>

    <script type="text/javascript" src="samplejs1.js"></script>

</head>

<body>

    

</body>

</html>

 

var a = 10;

var b = 20;

var linebreak = "<br />";

document.write ("((a > b) ? 100 : 200) => ");

result = (a > b) ? 100 : 200;

document.write(result);

document.write(linebreak);

 


Output



0 comments:

Post a Comment