Download presentation

Presentation is loading. Please wait.

Published byJuliet Lorrain Modified over 2 years ago

1
Operators and Expressions Operators are symbols that produce a result based on some rules. Examples: +, -, =, > and <. An operator manipulates data objects called operands. Binary operators manipulates two operands. Operands can be strings, numbers or Booleans.

2
Assignment Operator The assignment statement evaluates the expression on the right-hand side of the equal sign and assigns the result to the variable on the left-hand side of the equal sign. Ex: var result = 1 + 2;

3
Precedence and Associativity OperatorDescriptionAssociativity ()parenthesesLeft to Right ++; --auto increment; decrementRight to Left !logical NOTRight to Left *; /; %multiply;divide;modulusLeft to Right +; -add;subtractLeft to Right +concatenationLeft to Right <; <=Less-than; Less-than-equal- to Left to Right >; >=Greater-than; Greater-than- equal-to Left to Right ==; !=equal-to; not equal-toLeft to Right ===; !==identical to; not identical toLeft to Right &bitwise ANDLeft to Right

4
OperatorDescriptionAssoc. |bitwise ORLeft to Right ^bitwise XORLeft to Right ~bitwise NOTLeft to Right <

5
Example First JavaScript Sample var result = 5 + 4 * 12 / 4; document.write(“result = ” + result + " ");

6
Example 2 Precedence and Associativity var x = 5 + 4 * 12 / 4; document.write( " The result is " + x + " "); var x = ( 5 + 4 ) * ( 12 / 4 ); document.write("The result is " + x + " ");

7
Arithmetic Operators Operators/OperandsFunction x+yaddition x-ysubtraction x*ymultiplication x/ydivision (returns floating- point number) x%ymodulus

8
Example 3 Arithmetic Operators Arithmetic operators var num1 = 5; var num2 = 7; var result = num1 + num2; document.write(" num1 + num2 = "+ result ); result = result + (10 / 2 + 5); document.write(" 12 + (10 / 2 + 5) = " + result );

9
Shortcut Assignment Operators OperatorExampleMeaning =var x=1;assign 1 to variable “x” +=x+=2;add 2 to “x” and assign result to “x” -=x-=3;subtract 3 from “x” and assign result to “x” *=x*=4;multiply x by 4 and assign result to “x” /=x/=5;divide x by 5 and assign result to “x” %=x%=6;divide x by 6 and assign remainder to “x”

10
Example 4 Assignment and Shortcut Operators var num=10; document.write(" " + "num is assigned " + 10); num += 2; document.write(" num += 2; num is " + num ); num -= 1; document.write(" num -= 1; num is " + num); num *= 3; document.write(" num *= 3; num is " + num); num %= 5; document.write(" num %= 5; num is " + num);

11
Autoincrement and Autodecrement Operators (++) – increment the value of its operand by 1 (--) – decrement the value of its operand by 1 Prefixes: ++x; --x Postfixes: x++; x-- When used with assignment, they are different. y=0; x=5; y=++x; => y = 6 and x = 6 y=x++; => y = 5 and x = 6

12
Example 5 Auto-increment and Auto-decrement var x=5; var y=0; y = ++x; // add one to x first; then assign to y document.write(" Pre-increment: "); document.write("y is " + y + " "); document.write("x is " + x + " "); document.write("----------------------- "); var x=5; var y=0; y=x++; // assign value in x to y; then add one to x document.write(" Post-increment: "); document.write("y is " + y + " "); document.write("x is " + x + " ");

13
Concatenation Operator (+) – is used as concatenation operator. It is the only operator to manipulate strings. If the operands are a mix of strings and numbers, JavaScript will convert the numbers to string. (+=) – concatenates two strings and assigns the result to x, like: x=“hot”; x +=“dog”; x now is “hotdog”.

14
Comparison Operators Operator/OperandsFunction x==yx is equal to y x!=yx is not equal to y x>yx is greater than y x>=yx is greater or equal than y x

15
Equal and Identical Two strings are equal if they have the same length and same characters in corresponding position. Two numbers are equal if they have the same numeric value. Identical are not only the same value, but also of the same data type: “54”== 54 => true “54”===54 => false true==1 => true true===1 => false

16
Example 6 Comparing Numbers var x = 5; var y = 4; var result = x > y; document.writeln(" The result is "+ result + ". "); result = x < y; document.writeln( "The result is " + result + ". ");

17
Comparing strings - Example 7 Comparing Strings var fruit1 = "pear"; var fruit2 = "peaR"; var result = fruit1 > fruit2; document.writeln( " The result is "+ result + ". "); result = fruit1 < fruit2; document.writeln( "The result is " + result + ". "); result = fruit1 === fruit2; // Are they identical; i.e., value and type are the same? document.writeln( "The result is " + result + ". ");

18
Logical Operators Most often used in “if” statements. Numeric operand is true if it evaluates to any number that is not zero. Operator/OperandsFunction num1 && num2true if num1 is true and num2 is true num1 || num2true if num1 is true or num2 is true ! num1Not num1: true if num1 is false

19
Logical AND Whatever is suppose to happen is based on two conditions, and both conditions must be met. If the left hand expression of the AND operator is evaluate to zero, the expression is false and the right hand expression is not evaluated.

20
Example 8 Logical AND Operator var answer = prompt("How old are you? ", ""); if ( answer > 12 && answer < 20 ) { alert("Teenagers rock!"); }

21
Logical OR Whatever is suppose to happen is based in two conditions, and only one condition must be met. If the expression on the left hand side of the OR operator is evaluated to true, the value of the expression is true and the right hand side expression is not evaluated.

22
Example 9 Logical OR Operator var answer = prompt("Where should we eat? ", ""); if ( answer == "McDonald's" || answer == "Taco Bell" || answer == "Wendy's"){ alert("No fast food today, thanks."); }

23
Logical NOT It is used for a negation. The ! is called an unary operator because it has only one operand. !null => true !undefined => true

24
Example 10 Logical NOT Operator var answer = true; alert("Was ” + answer + “. Now " + !answer);

25
Example 11 Logical (Boolean) Operators var num1=50; var num2=100; var num3=0; document.write(" num1 && num2 is " + (num1 && num2) + ". "); document.write("num1 || $num2 is " + (num1 || num2) +". "); document.write("! num1 is " + !num1 +". "); document.write("!(num1 && num2) is " + !(num1 && num2) + ". "); document.write("!(num1 && num3) is " + !(num1 && num3) + ". ");

26
Conditional Operator It is called a ternary operator because it requires three operands. It is a short hand of the if/else conditional statement. x ? y : z If x evaluates to true, the value of the expression becomes y, else the value of the expression becomes z. big = (x > y) ? x : y;

27
Example 12 Conditional Operator var age = prompt("How old are you? ", ""); var price = (age > 55 ) ? 0 : 7.50; alert("You pay $" + price + 0);

28
Bitwise Operator Treat their operands as a set of 32 bits. OperatorFunction &Bitwise AND |Bitwise OR ^Bitwise XOR ~Bitwise NOT <

29
Example 13 Bitwise Operators Testing Bitwise Operators var result = 15 & 9; document.write("15 & 9 yields: " + result); result = 15 | 9; document.write(" 15 | 9 yields: " + result); result = 15 ^ 9; document.write(" 15 ^ 9 yields: " + result); result = 9 << 2; document.write(" 9 << 2 yields: " + result); result = 9 >> 2; document.write( " 9 >> 2 yields: " + result); result = -9 >> 2; document.write( " -9 >> 2 yields: " + result); result = 15 >>> 2; document.write( " 15 >>> 2 yields: " + result);

30
Datatype Conversion JavaScript is a loosely typed language. It automatically converts values when it assigns values to a variable or evaluates an expression. Sometimes you need to force conversions. JavaScript provides String(), Number() and Boolean() Methods.

31
Example 14 The Conversion Methods Data Conversion var num1 = prompt("Enter a number: ",""); var num2 = prompt("Enter another number: ",""); var result = Number(num1) + Number(num2); // Convert strings to numbers alert("Result is "+ result); var myString=String(num1); result=myString + 200; // String + Number is String alert("Result is "+ result); // Concatenates 200 to the result; alert("Boolean result is "+ Boolean(num2)); // Prints true

32
The parseInt() Method This Method converts a string to a number. It starts parsing at the beginning of the string and returns all integers until it reaches a non- integer and then stops parsing. In the two-argument format, the first argument is the string to be parsed and the second argument is the number base of the number.

33
Example 15 Using the parseInt() Function var grade = prompt("What is your grade? ", ""); // grade entered as a string grade=parseInt(grade); // grade converted to an integer document.write("grade type is " + typeof(grade)); grade+=10; document.write(" After a 10 point bonus, your grade is “ + grade + "! ");

34
The parseFloat() Method It is exactly like the pasrseInt() Method, except that it returns a floating point number.

35
Example 16 Using the parseFloat() Function var temp = prompt("What is your temperature? ", ""); temp=parseFloat(temp); if(temp == 98.6){ alert("Your temp is normal"); } else{ alert("You are sick!"); }

36
The eval() Method The eval() method evaluates a string of JavaScript statements and evaluates the whole thing as a little program, returning the result of the execution.

37
Example 17 The eval() Function var str="5 + 4"; var num1 = eval(str); var num2 = eval(prompt("Give me a number ", "")); alert(num1 + num2);

38
Special Operators Will be discussed later in the course. Some examples:, (comma) delete function instanceof new this void

Similar presentations

OK

2440: 211 Interactive Web Programming Expressions & Operators.

2440: 211 Interactive Web Programming Expressions & Operators.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on crash fire tender training Ppt on public speaking Ppt on brain machine interface Ppt on business plan of any product Ppt on life of william shakespeare Ppt on acid-base titration simulation Ppt on pricing policy benefits Ppt on india fdi Psychology ppt on motivation Ppt on supply chain management of a mule