CS101 Introduction To Computing: (Web Development Lecture 7)
CS101 Introduction To Computing: (Web Development Lecture 7)
CS101 Introduction To Computing: (Web Development Lecture 7)
Lecture 21
Data Types & Operators
(Web Development Lecture 7)
1
During the last lecture we had a discussion
on Objects, Properties, Methods
• Everything that JavaScript manipulates, it
treats as an object – e.g. a window or a button
A collection
All objects have the
of properties
“name” property: it
& methods
holds the name of
the object (collection)
name method 2
prop 1
prop 3 prop 5
prop 2
method 1 prop 4 method 3
3
Types of Objects
• JavaScript objects
– Objects that are part of JavaScript
– Examples: window, document
• Browser objects
– Objects that contain info not about the
contents of the display, but the browser itself
– Examples: history, navigator
• User-defined object
4
Object-Based, Not Object-Oriented!
• JavaScript is not a true object-oriented
language like C++ or Java
6
During Today’s Lecture …
• We will find out about JavaScript data types
7
JavaScript Data Types
• Unlike in C, C++ and Java, there are no explicit
data types in JavaScript
• Nevertheless, it recognizes & distinguishes
among the following types of values:
– Numbers, e.g., 23, 4.3, -230, 4.4e-24
– Booleans, e.g., true, false
– Strings, e.g., “hello”, “What’s the time?”
– Undefined 8
We’ll comeback to these data types,
but before that we have to have to
define a few new terms
First, variables:
9
Variables
• Variables give us the ability to manipulate data
through reference instead of actual value
x = 1;
while (x < 6) {
document.write (x);
x = x + 1;
}
11
Try Doing the Same Without Using A Variable
5 lines of code
replacing 5
lines of code!
12
Another Situation
x = 1;
while (x < 6000) {
document.write (x);
x = x + 1;
}
13
Declaring Variables
• Many languages require that a variable be
declared (defined) before it is first used
var height
15
JavaScript Variables are Dynamically Typed
number One
bhola@bholacontinental
19
numberOneUniversity
N99umber_one_University
_5numberoneuniversity
reallyReallyLongIndentifier12345678901234
20
Another Restriction on Identifiers
• Do not use any of the JavaScript keywords as
identifiers
25
Elements of JavaScript Statements
b=2; Identifiers
x = Math.floor ( x ) ; Punctuation
26
JavaScript Literals
• A data value that appears directly in a
statement
if ( tankFull == false)
addMoreWater = true
30
JavaScript Operators
• Operators operate on operands to achieve the
desired results
• JavaScript has numerous operators, classified
in many categories. We will look at only a few of
them belonging to the following categories:
– Assignment operators -- Arithmetic operators
– Comparison operators -- String operators
– Logical operators
total_number_of_students = 984 ;
swapFlag = false ;
x = y + 33 ;
32
Arithmetic Operators
Multiply 2*4→8
Divide 2 / 4 → 0.5
Modulus 5%2→1
Add 2+4→6
Subtract 2 - 4 → -2
Negate -(5) → -5 33
Comparison Operators
Not the same as
the assignment
“=” operator
The “equal to (==)” Comparison Operator
if ( today == “Sunday” )
document.write(“The shop is closed”);
if ( x != 0 )
result = y / x;
else
result = “not defined”;
36
From comparison operators, now we
move to Logical Operators
37
Logical Operators
Operate on Boolean expressions or variables
39
Example
if ( x || y )
document.write (“Either or both are true”);
else
document.write (“Both are false”);
40
So far we have looked at the assignment
operator, arithmetic operators, comparison
operators and logical operators
41
The “+” String Operator
a = 23 ;
quotient = floor( a / 2) ;
remainder = a % 2 ;
43
Elements of JavaScript Statements
b = 2; Identifiers
x = Math.floor ( x ); Punctuation
44
Two more elements that are found in
JavaScript statements are white
spaces and line breaks
45
White Spaces & Line Breaks
• White spaces: The space & the tab characters
46
while ( x > 0) {
remaind = x % 2;
y = remaind + y;
}
while ( x > 0) {
remaind = x % 2;
y = remaind + y;
} 47
Now let’s talk about a very special
type of JavaScript statement that
does not really do anything, but is
found in most pieces of code!
48
Comments
• Comments are included on a Web page to
explain how and why you wrote the page the
way you did
• Multi-line comments
/* Author: Bhola
Creation Date: 24 March 2003 */
50
HTML Comments
51
comments
add clarity!
52
comments let
the code speak
for itself!
53
Decimal to Binary Conversion in JavaScript
x = 75 ; // x is the decimal number
y = “” ; // y is the binary equivalent
while ( x > 0) {
remainder = x % 2 ;
quotient = Math.floor( x / 2 ) ;
y = remainder + y ;
x = quotient ;
}
document.write(“y = ” + y) ; 54
Assignment #7 (1)
Modify the Web page that you built for
assignment #4 so that it has the following
additional interactivity features:
57
Next (the 8th) Web Dev Lecture:
Flow Control and Loops