1 / 25

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?. Data Types. Common: Dates, Text, Numbers Other, more abstract, data types boolean, etc.

olin
Download Presentation

Variables and Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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?

  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

  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

  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'

  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)

  6. Demo – Handling Quotes Pause this slide and click the link below for a… video demonstration

  7. Exercise • Write a JavaScript program to display the following exact message (with the double-quotes) "Hey!", John said, "Don't do that!"

  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

  9. Boolean • A variable that can have a value of true or false is declared as a boolean. • Example… booleandidItWork; diditWork = true;

  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

  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

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

  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)

  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)

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

  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?

  17. Assigning Variables to Variables • var myVariable;var myOtherVariable;myOtherVariable = 22;myVariable = myOtherVariable;

  18. Basic String Operations • Concatenation • appending one string to another • var concatString = "Hello " + "Paul"; • Spaces are significant

  19. Mixing Numbers and Strings • You can freely concatenate numbers and strings (the numbers are converted to strings automatically)

  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

  21. Data Type Conversion • You may need to convert data from one type to another • String to integer [ parseInt( ) ] • String to floating points [ parseFloat( ) ]

  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?

  23. 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

  24. Null Values • varnullString = null; • varundefString; • varblankString = ""; //has a value of empty string

  25. End of Lesson

More Related