# 5.4 Operators

You will use lots of operators in expressions.  Earlier, you used the equal sign (=) as an assignment operator to assign a value to a variable.  In the preceding examples with strings, you used the plus symbol (+) to join two strings.  An operator generally performs some kind of calculation (operation) or  comparison with two values (the value on each side of an operator is called an operand ) to reach a third value.  In this lesson, you will see the different types of operators used in JavaScript programming.

Arithmatic Operators:

Operator Description Example Result
Substraction X=5Y=2x-y 3
* Multiplication X=5Y=4X*5 20
/ Division 15/55/2 32.5
% Modulus (division remainder) 5%210%810%2 120
++ Increment X=5X++ X=6
Decrement X=5x– X=4

<html>

<body>

<script type =”text/javascript”>

<!—

Var two =2

Var ten = 10

Var linebreak = “<br/>”

document write (“two plus ten =”)

result = two+ten

document.write (result)

document.write(linebreak)

document.write(“ten *ten =”)

result = ten*ten

document.write(result)

document.write(linebreak)

document.write (“ten/two =”)

result = ten/two

document.write (result)

// – – >

</script>

</body>

<html>

5.4.2 Assignment Operators

Operator Example Is The Same As
= X=y X=y
+= X+=y X=x+y
-= x-=y X=x-y
*= X*=y X=x*y
/= x/=y X=x/y
%= X%=y X=x%y

5.4.3 Comparison Operators

Operator Description Example
== Is equal to 5==8 returns false
=== Is equal to  (checks for both value and type) X=5Y=”5”
X==y returns trueX==y returns false
!= Is not equal 5!=8 returns true
> Is greater than 5>8 returns false
< Is less than 5<8 returns true
>= Is greater than or equal to 5>=8 returns false
<= Is less than or equal to 5<=8 returns true

5.4.4 Logical Operator

Operator Description Example
&& And X=6Y=3

(x<10&& y>1) returns true

II Or X=6Y=3

(x== II y==5) returns false

! not X=6Y=3

!(x==y) returns true

5.4.5 String Operator

The string operator concatenates one or more string using + operator

Txt1 = “have”

Txt2 = “a nice”

Txt3 = “day”

Txt4 = txt1+txt2+txt3;

The output of above example would be have a nice day

<html>

<title>

My script

</title>

<body>

<script type = “text/javascript”>

Document.write (“<h1> welcome to <br/> JavaScript + “<br/> programming!

<h1>”);

</script>

</body>

</html>

The above javascript program executes and displays following as output. Welcome to JavaScript programming

5.4.6 Conditional Assignment operator

Conditional operator checks a condition and then assigns a value to a variable.  The general syntax of the conditional assignment of operator is shown below:

Variable = (condition)? Value1 : Value2;

Here, condition is the logical expression

Value  1 is the value assigned to the variable when the logical expression is true.

Value  2 is the value assigned to the variable when the logical expression is false.

Example code snippet:

String result;

Result = (mark>40)? “pass”:”fail”;

In this example, the value “pass” is assigned when mare is greater than or equal to 40.  Other wise “fail” is assigned.

• • • • 