Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operators. Today’s Learning Goals … Understand the purpose of JavaScript operators Explore the mathematical operators Find out about the assignment operators.

Similar presentations


Presentation on theme: "Operators. Today’s Learning Goals … Understand the purpose of JavaScript operators Explore the mathematical operators Find out about the assignment operators."— Presentation transcript:

1 Operators

2 Today’s Learning Goals … Understand the purpose of JavaScript operators Explore the mathematical operators Find out about the assignment operators Understand the comparison operators Learn about the logical operators

3 What is an Operator? A short symbol in JavaScript that performs some sort of calculation, comparison, or assignment on one or more values

4 Types of Operators Mathematical Assignment Comparison Logical

5 Mathematical Operators Used for mathematical calculations Used for –Calculating sum of numbers –Joining strings together –Dividing numbers

6 OperatorSymbolFunction Addition+Adds two or more values Subtraction-Subtracts one value from another Multiplication*Multiplies two or more values Division/Divides one value by another Modulus%Divides one value by another and returns the remainder Increment++Shortcut to add 1 to a single number Decrement--Shortcut to subtract 1 from a single number Unary Negation -Makes a positive negative or a negative positive

7 Addition Operator (+) Variables for Addition Results <!-- var thesum = 4 + 7; window.alert(thesum); //-->

8 Addition Operator (+) Variables for Addition Results <!-- var num1 = 4; var num2 = 7; var thesum = num1 + num2; window.alert(thesum); //-->

9 Addition Operator (+) Type Conversions in Addition Calculations <!-- var num1 = 4.73; var num2 = 7; var thesum = num1 + num2; window.alert(thesum); //-->

10 Addition Operator (+) Type Conversions in Addition Calculations <!-- var num1 = 4; var num2 = “7”; var thesum = num1 + num2; window.alert(thesum); //-->

11 Subtraction Operator (-) <!-- var num1 = 10; var num2 = 3; var theresult = num1 - num2; window.alert(theresult); //-->

12 Multiplication Operator (*) <!-- var num1 = 4; var num2 = 5; var theresult = num1 * num2; window.alert(theresult); //-->

13 Division Operator (/) <!-- var num1 = 10; var num2 = 2; var theresult = num1 / num2; window.alert(theresult); //-->

14 Operand Value that the operator works on

15 Increment Operator (++) Before the Operand <!-- var num1 = 2; var theresult = ++num1; //-->

16 Increment Operator (++) After the Operand <!-- var num1 = 2; var theresult = num1++; //-->

17 Decrement Operator (--) Before the Operand <!-- var num1 = 2; var theresult = --num1; //-->

18 Decrement Operator (--) After the Operand <!-- var num1 = 2; var theresult = num1--; //-->

19 Unary Negation Operator (-) <!-- var negnum = -3; //-->

20 Assignment Operators Assign a value to a variable Do not compare two items, nor do they perform logical tests

21 OperatorSymbolFunction Assignment=Assigns the value on the RHS of the operator to a variable Add and assign+=Adds the value on the RHS of the operator to the variable on the LHS, and then assigns the new value to the variable Subtract and assign -=Subtracts the value on the RHS of the operator from the variable on the LHS, and then assigns the new value to the variable Multiply and assign *=Multiplies the value on the RHS of the operator by the variable on the LHS, and then assigns the new value to the variable Divide and assign /=Divides the variable on the LHS of the operator by the value on the RHS, and then assigns the new value to the variable Modulus and assign %=Takes the integer remainder of dividing the variable on the LHS by the value on the RHS, and assigns the new value to the variable

22 Assignment Operator (=) <!-- var population = 150000000; //-->

23 Add and Assign Operator (+=) <!-- var myMoney = 1000; myMoney = myMoney + 1 //-->

24 Add and Assign Operator (+=) <!-- var myMoney = 1000; myMoney += 1; //-->

25 Subtract and Assign Operator (-=) <!-- var myMoney = 1000; var myBills = 800; myMoney -= myBills; //-->

26 Multiply and Assign Operator (*=) <!-- var myMoney = 1000; var multby = 2; myMoney *= multby; //-->

27 Divide and Assign Operator (/=) <!-- var myMoney = 1000; var cutPayBy = 2; myMoney /= cutPayBy; //-->

28 Modulus and Assign Operator (%=) <!-- var myMoney = 1000; var cutPayBy = 2; myMoney %= cutPayBy; //-->

29 Comparison Operators Used with conditional statements and loops to perform actions only when a certain condition is met Return value of either true or false

30 OperatorSymbolFunction Is equal to==Returns true if the values on both sides of the operator are equal to each other Is not equal to!=Returns true if the values on both sides of the operator are not equal to each other Is greater than>Returns true if the value on the LHS of the operator is greater than the value on the RHS Is less than<Returns true if the value on the LHS of the operator is less than the value on the RHS Is greater than or equal to >=Returns true if the value on the LHS of the operator is greater than or equal to the value on the RHS Is less than or equal to <=Returns true if the value on the LHS of the operator is less than or equal to the value on the RHS

31 Is-Equal-To Operator (==) ComparisonReturn Value Reason 4==4 “my socks” == “my socks” 4==5 “my socks” == “My socks”

32 Is-Not-Equal-To Operator (!=) ComparisonReturn Value Reason 4 != 3 “CooL” != “cool” 4 != 4 “cool” != “cool”

33 Is-Greater-Than Operator (>) ComparisonReturn Value Reason 5 > 2 “a” > “A” 5 > 7 “A” > “a”

34 Is-Less-Than Operator (<) ComparisonReturn Value Reason 5 < 2 “a” < “A” 5 < 7 “A” < “a”

35 Is-Greater-Than-or-Equal-To Operator (>=) ComparisonReturn Value Reason 5 >= 2 “a” >= “A” 1 >= 2 “A” >= “a”

36 Is-Less-Than-or-Equal-To Operator (>=) ComparisonReturn Value Reason 2 <= 5 “A” <= “a” 5 <= 2 “a” <= “A”

37 Logical Operators Compare two conditional statements to see if one or more of the statements is true and to proceed accordingly Return either true or false

38 OperatorSymbolFunction AND&&Returns true if the comparisons on both sides of the operator are true OR||Returns true if the comparison on either side of the operator is true NOT!Used on a single comparison to make an expression that would normally return false true, or make an expression that would normally return true false

39 AND Operator (&&) ComparisonReturn Value Reason (1 == 1) && (2 == 2) (“A” <= “A”) && (“c” != “d”) (1 == 1) && (2 == 3) (“a” != “a”) && (“b” != “q”)

40 OR Operator (||) ComparisonReturn Value Reason (2 == 2) || (3 > 5) (5 > 17) || (4 != 9) (4 < 3) || (2 == 1)

41 NOT Operator (!) ComparisonReturn Value Reason ! (3 == 3) ! (2 > 5)

42 What We Learnt Today … Understood the purpose of JavaScript operators Explored the mathematical operators Found out about the assignment operators Understood the comparison operators Learnt about the logical operators


Download ppt "Operators. Today’s Learning Goals … Understand the purpose of JavaScript operators Explore the mathematical operators Find out about the assignment operators."

Similar presentations


Ads by Google