Download Data Types and Operators - Introduction to Computing - Lecture Slides and more Slides Introduction to Computing in PDF only on Docsity!
CS101 Introduction to Computing
Lecture 21
Data Types & Operators
(Web Development Lecture 7)
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
- An object has properties – e.g. a window has size, position, status, etc.
- An object can be manipulated with methods that are associated with that object – e.g. a resize a window with resizeTo(150, 200)
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
Object-Based, Not Object-Oriented!
- JavaScript is not a true object-oriented language like C++ or Java
- It is so because it lacks two key features:
- A formal inheritance mechanism
- Strong typing
- Nevertheless, JavaScript shares many similarities with object-oriented languages, and therefore is called an object-based language
During Today’s Lecture …
- We will find out about JavaScript data types
- About variables and literals
- We will also discuss various operators supported by JavaScript
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- - Booleans, e.g., true, false - Strings, e.g., “hello”, “What’s the time?” - Undefined (^8)
Variables
- Variables give us the ability to manipulate data through reference instead of actual value
- Variables are names assigned to values
- Variables are containers that hold values (Example: Hotel guest name, Guest room no.)
- Generally, the value of a variable varies during code execution (that is why the term “variable”!)
Example
x = 1;
while (x < 6) {
document.write (x);
x = x + 1;
11
x is a
variable
Another Situation
x = 1;
while (x < 6000) {
document.write (x);
x = x + 1;
Declaring Variables
- Many languages require that a variable be declared (defined) before it is first used
- Although JavaScript allows variable declaration, it does not require it - except in the case when we want to declare a variable being local (more on local variables later in the course!)
- However, it is good programming practice to declare variables before using them
JavaScript Variables are Dynamically Typed
- Any variable in JavaScript can hold any type of value, and the that type can change midway through the program
- This is unlike the case for C, C++ and Java, where a variable’s type is defined before usage
- The untyped feature makes JavaScript simpler to program in when developing short programs. However, this feature brings in a few problems as well. Can you describe any? (^16)
JavaScript Variables are Dynamically Typed
var sum ;
sum = 43 ;
sum = “empty” ;
17
After the execution of the 2nd statement, the data type becomes “number”
After the execution of the 3rd statement, the data type changes to “string”
After the execution of the 1st statement, the data type of the variable “sum” is “undefined”
1stStreet
number One
bhola@bholacontinental
numberOneUniversity
N99umber_one_University
_5numberoneuniversity
x
reallyReallyLongIndentifier