Upload
jemima-chambers
View
224
Download
1
Embed Size (px)
Citation preview
Module 5
JavaScript Operators
CS346 Javascript-5 2
Examples
JS-5 Examples
CS346 Javascript-5 3
Expressions
Expression — combination of literal values, variables, and operators that can be evaluated by JavaScript to produce a result
Operands — variables and literals contained in an expression
Operators — symbols used in expressions to manipulate operands
Performs some sort of calculation, comparison, or assignment on one or more values
CS346 Javascript-5 4
Operators
Arithmetic: + - * / % (int remainder)
Increment: ++++var — Value Incremented Immediately
var count = 1;var newCount = ++count; // newCount = 2
var++ — Value Incremented After Linevar count = 1;var newCount = count ++; // newCount = 1
Decrement: - - Similar to increment
CS346 Javascript-5 5
Operators
Assignment: = += -= *= /= %=var x=1, y=2;x += y; // x = 3
Comparison: == != > < >= <= Results in True or False
var x=1, y=2;x == y; // returns False
Logical: && || ! Results in True or False
var x=1, y=2;var returnVal = x==1 || y==5; // returns True
CS346 Javascript-5 6
Operators
Negation: - Bitwise: logical operators that work at
the bit level (ones and zeros) AND & XOR ^ OR | NOT - Left Shift << Right Shift >> Right Shift (Zero Fill) >>>
CS346 Javascript-5 7
Operators
String + Concatenate
var x=“You age is”, y=18;
var newStr = x + y;
document.write(newStr); // returns Your age is18
document.write(x + “ ” + y + “.”); // returns Your age is 18.
Conditional: boolean? exprT:exprFdocument.write(“The fee is ”+ (isMem ? “$5” :
“$10”));
Beware
Variable type is dynamic Examples
5-1AssignmentExamples.htm Combination of CSS and Javascript Generation of well-formed html document
5-2ComparisonExamples.htm 5-3LogicalExamples.htm