Title: Data Types and Variables
1Data Types and Variables
- Week 1 - Presentation 2
- Veronica Noone
2Objectives
- Numerical Data
- Text Data
- Escape Characters
- Boolean Data
- Variables
- Numerical Calculations
- Realistic Example
- Week 01 Lab 02 Pounds to Stone Calculator
3The What and Why of Data Types
- classification of data that tells the interpreter
how the programmer intends to use it. - Kinds of data are important
- 2 2
- Hello World
- JavaScript is Easygoing
- Weakly Typed Language
4Numerical Data
- Integers (whole numbers)
- 132
- -4
- Floating-point (fractions)
- 1.432
- -54.333
- They do have ranges but are vast
5Text Data
- String
- hello world
- A
- 123 Any Street
- What if you wanted to work with the phrase
- Todays quote is If at first you don't succeed
call it version 1.0
6Escape Characters
- Todays quote is \If at first you don\'t
succeed call it version 1.0\
7Boolean Data
- Can only hold one of two variables
- true or false
- Do you want to learn more about JavaScript?
- Yes? Click for the next slide
- No? Leave the room immediately
8Variables
- A named memory location where we can store data
- Declared with the keyword var
- Names are case sensitive
- No special characters
- No reserved words
- Should MEAN something
var exampleVariable
9Lets take a Look
ltscript language"javascript" type"text/javascrip
t"gt lt!--// // Declare my first variable var
myFirstVar //assign a value to
myFirstVar myFirstVar "Hello World" //display
myFirstVar in an alert box alert(myFirstVar) //
--gt lt/scriptgt
- Go to the class web page
- Right click on w01p02ex01.html and save the file
to your desktop
10Example Questions
- What is the data type of the variable myFirstVar?
- What would happened if we set the variable value
to something else? - Lets experiment
11Experiments
- Can you add a second variable? Give it a try!
- mySecondVar and set its value to 12345
- Now display it
- What if you?
- myFirstVar mySevcondVar
- Display it
12Numerical Calculations
- Basic Operators
- , -, and /
- Increment and Decrement
- myVar
- myVar myVar 1
- x- -
- x x 1
- Order of precedence applies
- Always use ()
13Lets take a Look
- Right click on w01p02ex02.html and save the file
to your desktop - Fill in the other examples
- Try a increment or decrement
- If this is easy for you PLEASE help you neighbor!
ltscript language"javascript" type"text/javascrip
t"gt lt!--// // example calculations var x var
y var ans //assign values x2 y4 //addition
example ansxy alert(ans) // --gtlt/scriptgt
14Questions
- What if you added string variables?
- Try it! add 2 more variables called s1 and s2.
- s1 hello
- s2 world
- Add then together and display with an alert.
- What happens? Space issue?
15Realistic Example
w01p02ex03.html
ltscript language"javascript" type"text/javascrip
t"gt lt!--// var weightInPounds var
weightInKilos //assign values //weightInPounds
150 weightInPounds prompt("Enter your
weight",150) weightInKilos weightInPounds0.453
59 //display myFirstVar in an alert
box document.write("You weigh " weightInKilos
"kg") // --gtlt/scriptgt
16New statements
- prompt() function
- prompt(text to be displayed,default)
- document.write()
- Document.write(whatever you want)
- Will cover more on this later
- Write is a method of the document object
17Week 01 Lab 02 Pounds to Stone Calculator
- Code JavaScript that asks the users to enter
their weight in pounds. - Convert pounds to stones and display on the page.
- Dont know what a stone is? Look it up!
- Lab worth 20 points
- Be sure to comment, use descriptive variable
names and format the page nicely. - Email me the link
- this will be the last time we email links - you
should have your web space set up so I can access
your past and present labs. - If that is already the case No email needed