25
Variables and Data Types • Data (information we're going to store) – Numbers – Text – Dates • What types of data can JavaScript process? • How do we store it? • How do we use it in code? 1

Variables and Data Types

  • Upload
    olin

  • View
    42

  • Download
    1

Embed Size (px)

DESCRIPTION

Variables and Data Types. Data (information we're going to store) Numbers Text Dates What types of data can JavaScript process? How do we store it? How do we use it in code?. Data Types. Common: Dates, Text, Numbers Other, more abstract, data types boolean, etc. - PowerPoint PPT Presentation

Citation preview

Page 1: Variables and Data Types

Variables and Data Types

• Data (information we're going to store)– Numbers– Text– Dates

• What types of data can JavaScript process?• How do we store it?• How do we use it in code?

1

Page 2: Variables and Data Types

2

Data Types

• Common: Dates, Text, Numbers• Other, more abstract, data types– boolean, etc.

• Strongly typed vs. weakly typed– JavaScript is weakly typed– Java is strongly typed

Page 3: Variables and Data Types

3

Numerical Data

• Integers - range from -2E53 to 2E53• Floating-point numbers (have a decimal place)• JavaScript treats these different when storing

them• JavaScript processes everything as if it were a

floating point• Hides the details from us

Page 4: Variables and Data Types

4

Text Data

• Another term for one or more characters is a String

• JavaScript recognizes strings (as different from code) because they are enclosed in quotes

• You may use single or double quotes– must end with what you started with– e.g. "Steve" or 'Steve', not "Steve'

Page 5: Variables and Data Types

5

Handling Quotes in the Text

• How do you enclose a quote in the text– "Peter O'Toole" or– 'Peter O\'Toole'

• The backslash is an escape character– tells the compiler to treat the next character as

text– or to do something special (e.g. advance to a

newline)

Page 7: Variables and Data Types

Exercise

• Write a JavaScript program to display the following exact message (with the double-quotes)

"Hey!", John said, "Don't do that!"

7

Page 8: Variables and Data Types

8

Special Characters

• \xNN– NN is a hexadecimal number which identifies a

character in the Latin-1 character set– huh?– see Appx. D for translation of characters– e.g. "\x9 Paul Wilton" will print as: (c) Paul Wilton

Page 9: Variables and Data Types

9

Boolean

• A variable that can have a value of true or false is declared as a boolean.

• Example…boolean didItWork;diditWork = true;

Page 10: Variables and Data Types

10

Variables - Storing Data

• Data stored permanently or temporarily• Permanent data is stored in a file or database• Temporary data is stored in a variable– variable values are lost when the page is left– held in computers memory (RAM)

• Example: Storing bank account information vs. loan payoff information

Page 11: Variables and Data Types

11

Variable Naming

• Rules for Naming:– case sensitive– can't use reserved words (see Appx. B)– can't use certain special characters• Example: &, % (see Appx. B)

– can use numbers, but not as the first character

Page 12: Variables and Data Types

12

Question

• Which of these names is a valid variable name?– with– myVariable99– my%Variable– my_Variable– my_Variable&Helper

Page 13: Variables and Data Types

13

Answer

– with (no, reserved word)– myVariable99 (yes)– my%Variable (no, special character)– my_Variable (yes, underscore _ is allowed)– my_Variable&Helper (no, & not allowed)

Page 14: Variables and Data Types

14

I Declare!

• You should declare the existence of a variable by using the "var" keyword

• e.g. var myFirstVariable;• Once declared, a variable can be store any

type of data (weakly typed)

Page 15: Variables and Data Types

15

Assigning Values

• Use the equals ( = ) sign• e.g. myFirstVariable = 101;

Page 16: Variables and Data Types

16

Garbage Collection

• The process of freeing up computer memory that was used to store variables is call "Garbage Collection"

• JavaScript does this automatically• What would happen if it didn't?

Page 17: Variables and Data Types

17

Assigning Variables to Variables

• var myVariable;var myOtherVariable;myOtherVariable = 22;

myVariable = myOtherVariable;

Page 18: Variables and Data Types

18

Basic String Operations

• Concatenation – appending one string to another– var concatString = "Hello " + "Paul";– Spaces are significant

Page 19: Variables and Data Types

19

Mixing Numbers and Strings

• You can freely concatenate numbers and strings (the numbers are converted to strings automatically)

Page 20: Variables and Data Types

20

Exercise

• Code a JavaScript program that declares a variable that holds an age (in years) and a variable that holds a person's name and stores them in a variable in the following format…

Hey Steve, you are 54 years old

• Display this message in an alert box

Page 21: Variables and Data Types

21

Data Type Conversion

• You may need to convert data from one type to another– String to integer [ parseInt( ) ]– String to floating points [ parseFloat( ) ]

Page 22: Variables and Data Types

22

Exercise

• Write a JavaScript program that prompts a user for their age

• Now substract 10 from the user's answer• And display a message in the following

format…

Are you really only 44 years old?

Page 23: Variables and Data Types

23

Data that Won't Convert

• var myString = "I'm a name not a number!";• Returns NaN– Special Value– means Not a Number

• isNaN( ) function– e.g. myVar1 = isNaN("Hello");• TRUE

– myVar2 = isNaN(34)• FALSE

Page 24: Variables and Data Types

Null Values

• var nullString = null;• var undefString;

• var blankString = ""; //has a value of empty string

24

Page 25: Variables and Data Types

End of Lesson

25