You are on page 1of 14

GITA/6th Semester/CSE-I/IWT/JAVASCRIPT/OPERATORS Prepared by:Er Chinmaya Kumar Nayak (Asst.

Professor)

Date:08/04/12

JavaScript Arithmetic Operators


Arithmetic operators are used to perform arithmetic between variables and/or values. Given that y=5, the table below explains the arithmetic operators: Operator + * / % ++ -Description Addition Subtraction Multiplication Division Modulus (division remainder) Increment Decrement Example x=y+2 x=y-2 x=y*2 x=y/2 x=y%2 x=++y x=y++ x=--y x=y-Result x=7 x=3 x=10 x=2.5 x=1 x=6 x=5 x=4 x=5 y=5 y=5 y=5 y=5 y=5 y=6 y=6 y=4 y=4

JavaScript Assignment Operators


Assignment operators are used to assign values to JavaScript variables. Given that x=10 and y=5, the table below explains the assignment operators: Operator = += -= *= /= %= Example x=y x+=y x-=y x*=y x/=y x%=y Same As x=x+y x=x-y x=x*y x=x/y x=x%y Result x=5 x=15 x=5 x=50 x=2 x=0

The + Operator Used on Strings


The + operator can also be used to add string variables or text values together. To add two or more string variables together, use the + operator. txt1="What a very"; txt2="nice day"; txt3=txt1+txt2;
1

GITA/6th Semester/CSE-I/IWT/JAVASCRIPT/OPERATORS Prepared by:Er Chinmaya Kumar Nayak (Asst. Professor)

Date:08/04/12

After the execution of the statements above, the variable txt3 contains "What a verynice day". To add a space between the two strings, insert a space into one of the strings: txt1="What a very "; txt2="nice day"; txt3=txt1+txt2; or insert a space into the expression: txt1="What a very"; txt2="nice day"; txt3=txt1+" "+txt2; After the execution of the statements above, the variable txt3 contains: "What a very nice day"

Adding Strings and Numbers


The rule is: If you add a number and a string, the result will be a string!

Example
x=5+5; document.write(x); x="5"+"5"; document.write(x); x=5+"5"; document.write(x); x="5"+5; document.write(x); Comparison and Logical operators are used to test for true or false.

GITA/6th Semester/CSE-I/IWT/JAVASCRIPT/OPERATORS Prepared by:Er Chinmaya Kumar Nayak (Asst. Professor)

Date:08/04/12

Comparison Operators
Comparison operators are used in logical statements to determine equality or difference between variables or values. Given that x=5, the table below explains the comparison operators:
Operator == === != > < >= <= Description is equal to is exactly equal to (value and type) is not equal is greater than is less than is greater than or equal to is less than or equal to Example x==8 is false x==5 is true x===5 is true x==="5" is false x!=8 is true x>8 is false x<8 is true x>=8 is false x<=8 is true

How Can it be Used


Comparison operators can be used in conditional statements to compare values and take action depending on the result:
if (age<18) document.write("Too young");

You will learn more about the use of conditional statements in the next chapter of this tutorial.

GITA/6th Semester/CSE-I/IWT/JAVASCRIPT/OPERATORS Prepared by:Er Chinmaya Kumar Nayak (Asst. Professor)

Date:08/04/12

Logical Operators
Logical operators are used to determine the logic between variables or values. Given that x=6 and y=3, the table below explains the logical operators:
Operator && || ! Description and or not Example (x < 10 && y > 1) is true (x==5 || y==5) is false !(x==y) is true

Conditional Operator
JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
Syntax variablename=(condition)?value1:value2 Example

Example
If the variable visitor has the value of "PRES", then the variable greeting will be assigned the value "Dear President " else it will be assigned "Dear":
<script type="text/javascript"> var visitor="PRES"; var greeting=(visitor=="PRES")?"Dear President ":"Dear "; document.write(greeting); </script>

Conditional statements are used to perform different actions based on different conditions.

GITA/6th Semester/CSE-I/IWT/JAVASCRIPT/OPERATORS Prepared by:Er Chinmaya Kumar Nayak (Asst. Professor)

Date:08/04/12

Example1: left_operand = right_operand var myVar = "Welcome to the circus!"; document.write(myVar); document.write("<hr />"); myVar = "Welcome to the zoo!"; document.write(myVar); OUTPUT: Welcome to the zoo!

Example2: Addition var a = 3; var b = 2; var c = a + b; document.write(c); OUTPUT: 5 Example3: String Concatenation var user = "Henry"; var country = "USA"; var output = user + " is from " + country; document.write(output); OUTPUT: Henry is from USA

Example4: left_operand += right_operand,Append to a string or variable var myVar = "<h2>Welcome to my how-to page</h2>";
5

GITA/6th Semester/CSE-I/IWT/JAVASCRIPT/OPERATORS Prepared by:Er Chinmaya Kumar Nayak (Asst. Professor)

Date:08/04/12

myVar += "<h3>I Will Explain How To Do This Thing</h3>"; myVar += "<p>First, grab yourself a new clean ...<p>"; myVar += "<p>Second, be sure to insert it firmly into ...<p>"; document.write(myVar); OUTPUT:

Welcome to my how-to page


I Will Explain How To Do This Thing

First, grab yourself a new clean ... Second, be sure to insert it firmly into ...

Example5: Simple shorthand arithmetic example var a = 2; var b = 3; document.write(a += b); OUTPUT: 5

Example6: left_operand == right_operand var a = 4; var b = 3; document.write(a == b); OUTPUT: False

GITA/6th Semester/CSE-I/IWT/JAVASCRIPT/OPERATORS Prepared by:Er Chinmaya Kumar Nayak (Asst. Professor)

Date:08/04/12

Example6.1: You will see it in use most commonly within if...else condition statements. var a = 4; var b = 3; if (a == b){ document.write("YES THAT IS TRUE"); } else { document.write("NO THAT IS FALSE"); } OUTPUT: NO THAT IS FALSE

Example7: left_operand === right_operand var v1 = "5"; var v2 = 5; if (v1 === v2){ document.write("TRUE"); } else { document.write("FALSE"); } OUTPUT: False

GITA/6th Semester/CSE-I/IWT/JAVASCRIPT/OPERATORS Prepared by:Er Chinmaya Kumar Nayak (Asst. Professor)

Date:08/04/12

Example8: var v1 = "5"; var v2 = 5; if (v1 == v2){ document.write("TRUE"); } else { document.write("FALSE"); } OUTPUT: True

Example9: left_operand != right_operand var v1 = 30; var v2 = 50; document.write(v1 != v2); OUTPUT: True

Example10: left_operand !== right_operand var v1 = "50"; var v2 = 50; document.write(v1 !== v2); OUTPUT: True

GITA/6th Semester/CSE-I/IWT/JAVASCRIPT/OPERATORS Prepared by:Er Chinmaya Kumar Nayak (Asst. Professor)

Date:08/04/12

Example11: var v1 = "50"; var v2 = 50; document.write(v1 != v2); OUTPUT: False Example12: left_operand < right_operand var v1 = 4; var v2 = 7; document.write(v1 < v2); OUTPUT: True Example13: var v1 = "abc"; var v2 = "def"; document.write(v1 < v2); OUTPUT: True Example14: left_operand > right_operand var v1 = 4; var v2 = 7; document.write(v1 > v2); OUTPUT: False Example15: var v1 = "abc"; var v2 = "def"; document.write(v1 > v2);
9

GITA/6th Semester/CSE-I/IWT/JAVASCRIPT/OPERATORS Prepared by:Er Chinmaya Kumar Nayak (Asst. Professor)

Date:08/04/12

OUTPUT: False Example16: left_operand <= right_operand var v1 = 5; var v2 = 5; document.write(v1 <= v2); OUTPUT: True Example17: var v3 = "c"; var v4 = "d"; document.write(v3 <= v4); OUTPUT: True Example18: (expression) ? "output for true" : "output for false" var a = 5; var b = 3; var result = (a > b) ? "that is true" : "that is false"; document.write(result); OUTPUT: that is true

Example19: var a = 5; var b = 3; function funcForTrue(){ document.write("Executed function for a TRUE result"); }
10

GITA/6th Semester/CSE-I/IWT/JAVASCRIPT/OPERATORS Prepared by:Er Chinmaya Kumar Nayak (Asst. Professor)

Date:08/04/12

function funcForFalse(){ document.write("Executed function for a FALSE result"); } var result = (a > b) ? funcForTrue() : funcForFalse(); OUTPUT: Executed function for a TRUE result Example20: increment operator ( ++ ) var i = 0; i++; document.write(i); OUTPUT: 1

Example21: for (var i = 0; i < 5; i++) { document.write(i + "<br />"); } OUTPUT: 0 1 2 3 4

Example22: modulus operator ( % ) for (var i = 1; i <= 20; i++) { if(i % 2 == 0){ // if value is divisible by the number 2 make it red document.write("<span style='color:red;'>"+i+"</span><br />"); } else { document.write(i + "<br />");
11

GITA/6th Semester/CSE-I/IWT/JAVASCRIPT/OPERATORS Prepared by:Er Chinmaya Kumar Nayak (Asst. Professor)

Date:08/04/12

} } OUTPUT: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Example23:

Conditional Statements
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In JavaScript we have the following conditional statements:

if statement - use this statement to execute some code only if a specified condition is true

if...else statement - use

12

GITA/6th Semester/CSE-I/IWT/JAVASCRIPT/OPERATORS Prepared by:Er Chinmaya Kumar Nayak (Asst. Professor)


Date:08/04/12

this statement to execute some code if the condition is true and another code if the condition is false if...else if....else statement - use this statement to select one of many blocks of code to be executed switch statement - use this statement to select one of many blocks of code to be executed

If Statement
Use the if statement to execute some code only if a specified condition is true.
Syntax if (condition) { code to be executed if condition is true }

Note that if is written in lowercase letters. Using uppercase letters (IF) will generate a JavaScript error!

Example
<script type="text/javascript"> //Write a "Good morning" greeting if //the time is less than 10 var d=new Date(); var time=d.getHours(); if (time<10) { document.write("<b>Good morning</b>"); } </script>

If...else Statement
Use the if....else statement to execute some code if a condition is true and another code if the condition is not true.
Syntax if (condition) { 13

GITA/6th Semester/CSE-I/IWT/JAVASCRIPT/OPERATORS Prepared by:Er Chinmaya Kumar Nayak (Asst. Professor) code to be executed if condition is true } else { code to be executed if condition is not true }

Date:08/04/12

Example
<script type="text/javascript"> //If the time is less than 10, you will get a "Good morning" greeting. //Otherwise you will get a "Good day" greeting. var d = new Date(); var time = d.getHours(); if (time < 10) { document.write("Good morning!"); } else { document.write("Good day!"); } </script>

14

You might also like