# Operators and Expressions Operators are symbols that produce a result based on some rules. Examples: +, -, =, > and <. An operator manipulates data objects.

## Presentation on theme: "Operators and Expressions Operators are symbols that produce a result based on some rules. Examples: +, -, =, > and <. An operator manipulates data objects."— Presentation transcript:

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.

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;

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

OperatorDescriptionAssoc. |bitwise ORLeft to Right ^bitwise XORLeft to Right ~bitwise NOTLeft to Right <>bitwise right shiftLeft to Right >>>bit zero-filled,right shiftLeft to Right &&logical ANDLeft to Right ||logical ORLeft to Right ?:ternary conditionalRight to Left =; +=; -=; *=; /=; %=;assignmentRight to Left,(comma)

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

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 + " ");

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

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 );

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”

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);

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

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 + " ");

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”.

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/3383374/slides/slide_14.jpg", "name": "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 xyx is greater than y x>=yx is greater or equal than y x

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

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 + ". ");

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 + ". ");

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

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.

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

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.

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."); }

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

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) + ". ");

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;

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

Bitwise Operator Treat their operands as a set of 32 bits. OperatorFunction &Bitwise AND |Bitwise OR ^Bitwise XOR ~Bitwise NOT <>Right shift >>>Zero-fill right shift

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);

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.

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

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.

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

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!"); }

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.

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

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

Download ppt "Operators and Expressions Operators are symbols that produce a result based on some rules. Examples: +, -, =, > and <. An operator manipulates data objects."

Similar presentations