JavaScript Lecture 2 : topics to be covered - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

JavaScript Lecture 2 : topics to be covered

Description:

TA websites (TA Email list page) For examples of code. Assignment statement ... vis- -vis || section 3 && section 5 'all sections greater than 3 and less than 5' ... – PowerPoint PPT presentation

Number of Views:90
Avg rating:3.0/5.0
Slides: 24
Provided by: remusR
Category:

less

Transcript and Presenter's Notes

Title: JavaScript Lecture 2 : topics to be covered


1
JavaScript Lecture 2 topics to be covered
  • Assignment/ recitation topics/ late-box
  • Appendices
  • Review
  • Assignment
  • Expression
  • Operators

2
Visit.
  • www.w3schools.com
  • For JavaScript and HTML tutorials
  • TA websites (TA Email list page)
  • For examples of code

3
Assignment statement
  • ltvariablegt ltassignment symbolgt ltexpressiongt
  • Eg. Area pi r r

4
Expressions
  • Variables
  • Operators
  • Arithmetic
  • Relational
  • Logical

5
Arithmetic operators
  • Symbols of basic arithmetic
  • Division /
  • Multiplication
  • Addition
  • Subtraction
  • Operator precedence

6
  • All of the above are binary operators
  • ab , ab, a/b, a-b
  • a and b are known as operands
  • Unary operators involve one operand
  • Eg. Negate (-)
  • -a, -4

7
Modulus operator(mod)
  • Divides the two integers and returns the
    remainder as result
  • Eg. 5 2 1
  • 4 2 0
  • Division returns the quotient
  • 4 / 2 2

8
  • ltScript Language"JavaScript"gt
  •   var EarningsJob1, EarningsJob2 
  • var TotalForTheWeek
  • EarningsJob1 325.38
  •   EarningsJob2 580.64
  •   TotalForTheWeek EarningsJob1
    EarningsJob2
  • document.write("ltpgtSalarieslt/pgt") 
  • document.write("Job 1")
  •   document.write(EarningsJob1)
  •  
  • document.write("ltbrgt") 
  • document.write("Job 2 ")
  •   document.write(EarningsJob2)
  •  
  • document.write("lthr alignleft width120
    colorFF5758gt")

9
(No Transcript)
10
Relational Operators
  • Make comparison between two values
  • Returns Boolean value - either true or false
  • a lt b Is a less than b?
  • a lt b Is a less than or equal to b?
  • a b Is a equal to b?
  • a ! b Is a not equal to b?
  • a gt b Is a greater than or equal to b?
  • a gt b Is a greater than b?

11
  • Eg.
  • salary gt 1000
  • age gt 18
  • course cs170

12
Logical Operators
  • test two or more relationships together
  • course cs170 and section lt8
  • course cs170 and section lt7
  • AND
  • OR
  • NOT

13
Logical AND operator
  • exp1 exp 2
  • outcome is true if the outcome for both exp1 and
    exp2 are true
  • course cs170 and section lt8
  • in JavaScript ? course cs170 section lt8
  • course cs170 and section lt7
  • in JavaScript ? course cs170 section lt7

14
Logical OR operator
  • exp1 exp 2
  • outcome is true if the outcome for either exp1
    and exp2 are true
  • section 3 section 6

15
vis-à-vis
  • section gt3 section lt5
  • all sections greater than 3 and less than 5
  • (this shall be true only for section 4)
  • section gt3 section lt5
  • all sections greater than 3 or less than 5
  • (this shall be true for all sections)

16
Logical NOT operator
  • !
  • Unary operator
  • !(expression)
  • Outcome is the opposite of the value of
    operand
  • !(section lt 3 section gt 5)
  • True for 4

17
..looking back
  • / -
  • lt lt ! gt gt
  • !

18
Operator Overload
  • Use of an operator with different data types
  • 4 5
  • Outcome 9
  • Overload an operator to operate on other data
    types.
  • four five
  • Outcome fourfive

19
Example of operator overload - concatenation
  • operator is overloaded
  • For string operands
  • Concats or joins the two string operands together
  • 4 5 Outcome 9
  • four five Outcome fourfive
  • 4 5 Outcome 45

20
  • document.write (four five)
  • outcome fourfive
  • var name Jim
  • document.write (employee name name)
  • outcome employee name Jim

21
  • var name Jim
  • document.write (employee nameltbgt name
    lt/bgt)
  • outcome employee name Jim

22
  • lthtmlgt
  • ltheadgt
  • lt/headgt
  • ltbodygt
  • ltscript type"text/javascript"gt
  • var name Jim"
  • document.write(Name name)
  • name Jane"
  • document.write(Nameltugt"name"lt/ugt")
  • document.write("ltbgt regular HTML text
    lt/bgtltbrgt")
  • var i2
  • document.write(i"ltbrgt")
  • i105
  • document.write(i)
  • lt/scriptgt
  • lt/bodygt

23
  • Name Jim
  • Name Jane
  • regular HTML text
  • 2
  • 105
Write a Comment
User Comments (0)
About PowerShow.com