Download presentation
Presentation is loading. Please wait.
Published byJemima O’Brien’ Modified over 9 years ago
1
Operators © Copyright 2014, Fred McClurg All Rights Reserved
2
Basic Arithmetic Operators Examples: 2 + 5; // 7 4 - 3; // 1 5 - 9; // -4 3 * 4; // 12 36 / 6; // 6 36 / 5; // 7.2 2 arithOper.html
3
3 Modulo Defined Discussion: The modulo is the integer remainder result from the division of two numbers. Example: 5 / 2 = 2 with a remainder (or modulo) of 1. Purpose: Modulo is often used to determine if a value is an even multiple. Modulo is also used to determine if a value is an even or odd number (multiple of two).
4
Modulo Operator Examples // remainder of 1 after division console.log( 21 % 5 ); // 1 /* 5 is a multiple of 20 because it's evenly divisible by 5 */ console.log( 20 % 5 ); // 0 /* 21 is an odd number because it is not evenly divisible by 2 */ console.log( 21 % 2 ); // 1 /* 20 is an even number because it is evenly divisible by 2 */ console.log( 20 % 2 ); // 0 4 moduloOper.html
5
String Operator Discussion: The plus “ + ” symbol can also be used to concatenate (i.e. join) multiple strings together. Examples: // spiderman (concatenation) var hero = "spider" + "man"; console.log( hero ); 5 strOper.html
6
6 Operator Precedence Discussion: Every operator has a precedence. The order in which arithmetic is performed is based upon that precedence. The parenthesis “()” is used to force precedence. Examples: // default precedence order // multiplication is performed first 4 + 4 * 10; // 44 // use parenthesis to force precedence ( 4 + 4 ) * 10; // 80 operOrder.html
7
Numeric Short Cut Operators Discussion: The Short Cut Operators (also known as Assignment Operators) are available for all arithmetic operators. Examples: var browser = 0; count += 4; // count = count + 4; count -= 2; // count = count - 2; count *= 3; // count = count * 3; count /= 2; // count = count / 2; count %= 2; // count = count % 2; 7 shortOpNum.html
8
Discussion: The “ += ” Short Cut Operator (also known as Assignment Operator) also works to concatenate strings. Examples: var quote = ""; // initialize value quote += "Please accept my resignation. "; quote += "I don't want to belong "; quote += "to any club that will accept "; quote += "people like me as a member. "; quote += "-- Groucho Marx"; String Short Cut Operator 8 shortOpStr.html
9
Unary Operators: Increment and Decrement Discussion: The Increment Operator “ ++ ” and Decrement Operator “ -- ” is a Short Cut Operator for adding and subtracting one from a value. Examples: var countInc = 0; countInc++; // count = count + 1; console.log( countInc ); // 1 var countDec = 2; countDec--; // count = count - 1; console.log( countDec ); // 1 9 unaryOper.html
10
Prefix Increment Operator Discussion: The Prefix Increment Operator “ ++x ” performs the addition first and then the assignment. Example: var count = 0; // Prefix order of operation: // 1. count + 1 // 2. prefix = count var prefix = ++count; console.log( count ); // 1 console.log( prefix ); // 1 10 prefixIncOp.html
11
Postfix Increment Operator Discussion: The Postfix Increment Operator “ x++ ” performs the assignment first and then the addition. Example: var index = 0; // Postfix order of operation: // 1. postfix = index // 2. index + 1 var postfix = index++; console.log( postfix ); console.log( index ); 11 postfixIncOp.html Question: What is the value of prefix?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.