340 likes | 836 Views
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.
E N D
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. • Strongly typed vs. weakly typed • JavaScript is weakly typed • Java is strongly typed
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
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'
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)
Demo – Handling Quotes Pause this slide and click the link below for a… video demonstration
Exercise • Write a JavaScript program to display the following exact message (with the double-quotes) "Hey!", John said, "Don't do that!"
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
Boolean • A variable that can have a value of true or false is declared as a boolean. • Example… booleandidItWork; diditWork = true;
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
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
Question • Which of these names is a valid variable name? • with • myVariable99 • my%Variable • my_Variable • my_Variable&Helper
Answer • with (no, reserved word) • myVariable99 (yes) • my%Variable (no, special character) • my_Variable (yes, underscore _ is allowed) • my_Variable&Helper (no, & not allowed)
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)
Assigning Values • Use the equals ( = ) sign • e.g. myFirstVariable = 101;
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?
Assigning Variables to Variables • var myVariable;var myOtherVariable;myOtherVariable = 22;myVariable = myOtherVariable;
Basic String Operations • Concatenation • appending one string to another • var concatString = "Hello " + "Paul"; • Spaces are significant
Mixing Numbers and Strings • You can freely concatenate numbers and strings (the numbers are converted to strings automatically)
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
Data Type Conversion • You may need to convert data from one type to another • String to integer [ parseInt( ) ] • String to floating points [ parseFloat( ) ]
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?
Data that Won't Convert • varmyString = "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
Null Values • varnullString = null; • varundefString; • varblankString = ""; //has a value of empty string