Download presentation
Presentation is loading. Please wait.
1
Chapter 19 JavaScript
2
JavaScript Interpretive programming language
It’s designed to work inside web pages uses <script> tag Ex. <script language=“javascript”> </script> It can be embedded in anywhere in the XHTML code Nesting <script> tag generates a syntax error Case sensitive: sum, Sum, SUM are different
3
JavaScript You need to know three major things to learn a programming language now Language Syntax/Rules Event handling Object Oriented Programming (OOP) concept
4
JavaScript Language Syntax/Rules
Variable: symbolic name can store a value Identifiers: begin with letter, underscore, or dollar sign Types: number = 30, 3.14, -25, -2.4 Boolean = true or false (reserved words) String = “25 dollars”, ‘totalvalue’ Var sum = 100; Name = “tim”; Scope: global V.S. local (“Var” statement) Constants: const pi = 3.14 Special character: \
5
JavaScript Statements: a line of JavaScript code
Use “=“ sign -> x = 20; y = x; Must end with a “;” -> x = y + 20; Must not be broken in to two lines. Comments: // inline comments /* block comments */
6
JavaScript Expressions & Operators
Assignment operators “=“, “+=“, “-=“, “*=“, “/=“ y = 30; x = x + y; x += y; Comparison operators == , !=, ===, !==, >, >=, <, <= A = (x == y); B = (z >= 20); Arithmetic operators +, -, *, /, %, ++, - -, and – (negative) x=10; ++x; x++; y=5; y- -; y++ Logical Operators && (and), II (or), ! (not) (T && F) (F && T) (F && F) (F || F) (! T) = False (T || F) (F || T) (T || T) (T && T) (! F) = True x = true; y=false; z = !( x && y); z = ?
7
JavaScript Control Structures - Conditional (if and switch)
if (condition) {true block } else {false block} switch (expression) { case label1: statement; break; case label2: statement; break; default statement; } *see example 19.5
8
JavaScript Control Structures – loops for loop:
Loops need a control value/counter to set the looping time for loop: for (initial value of counter; condition; increment) {looping statements} for (counter=0; counter<10; counter++) {document.write (“for loop = “ + counter + “<br/>”);} How many time does this loop go?
9
JavaScript Control Structures – loops while loop:
while (condition) {{looping statements} counter = 0; while (counter < 10) {document.write (“while loop= “ + counter + “<br/>”); counter++; }// How many time does this loop go? See example 19.6, 19.7
10
JavaScript Control Structures – loops do while loop:
do {looping statements} while (condition) counter = 0; do {document.write (“do while= “ + counter + “<br/>”); counter++; } while (counter < 10) How many time does this loop go?
11
JavaScript Control Structures – loops
infinite loops: loop does not have a control value/counter to set the looping time
12
JavaScript document.write() // for web page output
Window input and output method You should not put XHTML tags in output string Id = prompt(“string”, “initial value”) get_prompt = prompt(“click yes”, 7); Id = comfirm(“string”); Answer = comfirm(“the number is “ + x); alert (“string”) alert (“the answer is “ + x); document.write() // for web page output
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.