Operators
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
What is an Operator? • A short symbol in JavaScript that performs some sort of calculation, comparison, or assignment on one or more values
Types of Operators • Mathematical • Assignment • Comparison • Logical
Mathematical Operators • Used for mathematical calculations • Used for • Calculating sum of numbers • Joining strings together • Dividing numbers
Addition Operator (+)Variables for Addition Results <SCRIPT language = “JavaScript”> <!-- var thesum = 4 + 7; window.alert(thesum); //--> </SCRIPT>
Addition Operator (+)Variables for Addition Results <SCRIPT language = “JavaScript”> <!-- var num1 = 4; var num2 = 7; var thesum = num1 + num2; window.alert(thesum); //--> </SCRIPT>
Addition Operator (+)Type Conversions in Addition Calculations <SCRIPT language = “JavaScript”> <!-- var num1 = 4.73; var num2 = 7; var thesum = num1 + num2; window.alert(thesum); //--> </SCRIPT>
Addition Operator (+)Type Conversions in Addition Calculations <SCRIPT language = “JavaScript”> <!-- var num1 = 4; var num2 = “7”; var thesum = num1 + num2; window.alert(thesum); //--> </SCRIPT>
Subtraction Operator (-) <SCRIPT language = “JavaScript”> <!-- var num1 = 10; var num2 = 3; var theresult = num1 - num2; window.alert(theresult); //--> </SCRIPT>
Multiplication Operator (*) <SCRIPT language = “JavaScript”> <!-- var num1 = 4; var num2 = 5; var theresult = num1 * num2; window.alert(theresult); //--> </SCRIPT>
Division Operator (/) <SCRIPT language = “JavaScript”> <!-- var num1 = 10; var num2 = 2; var theresult = num1 / num2; window.alert(theresult); //--> </SCRIPT>
Operand • Value that the operator works on
Increment Operator (++)Before the Operand <SCRIPT language = “JavaScript”> <!-- var num1 = 2; var theresult = ++num1; //--> </SCRIPT>
Increment Operator (++)After the Operand <SCRIPT language = “JavaScript”> <!-- var num1 = 2; var theresult = num1++; //--> </SCRIPT>
Decrement Operator (--)Before the Operand <SCRIPT language = “JavaScript”> <!-- var num1 = 2; var theresult = --num1; //--> </SCRIPT>
Decrement Operator (--)After the Operand <SCRIPT language = “JavaScript”> <!-- var num1 = 2; var theresult = num1--; //--> </SCRIPT>
Unary Negation Operator (-) <SCRIPT language = “JavaScript”> <!-- var negnum = -3; //--> </SCRIPT>
Assignment Operators • Assign a value to a variable • Do not compare two items, nor do they perform logical tests
Assignment Operator (=) <SCRIPT language = “JavaScript”> <!-- var population = 150000000; //--> </SCRIPT>
Add and Assign Operator (+=) <SCRIPT language = “JavaScript”> <!-- var myMoney = 1000; myMoney = myMoney + 1 //--> </SCRIPT>
Add and Assign Operator (+=) <SCRIPT language = “JavaScript”> <!-- var myMoney = 1000; myMoney += 1; //--> </SCRIPT>
Subtract and Assign Operator (-=) <SCRIPT language = “JavaScript”> <!-- var myMoney = 1000; var myBills = 800; myMoney -= myBills; //--> </SCRIPT>
Multiply and Assign Operator (*=) <SCRIPT language = “JavaScript”> <!-- var myMoney = 1000; var multby = 2; myMoney *= multby; //--> </SCRIPT>
Divide and Assign Operator (/=) <SCRIPT language = “JavaScript”> <!-- var myMoney = 1000; var cutPayBy = 2; myMoney /= cutPayBy; //--> </SCRIPT>
Modulus and Assign Operator (%=) <SCRIPT language = “JavaScript”> <!-- var myMoney = 1000; var cutPayBy = 2; myMoney %= cutPayBy; //--> </SCRIPT>
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
Is-Equal-To Operator (==)
Is-Not-Equal-To Operator (!=)
Is-Greater-Than Operator (>)
Is-Less-Than Operator (<)
Is-Greater-Than-or-Equal-To Operator (>=)
Is-Less-Than-or-Equal-To Operator (>=)
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
AND Operator (&&)
OR Operator (||)
NOT Operator (!)
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