Presentation is loading. Please wait.

Presentation is loading. Please wait.

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?

Similar presentations


Presentation on theme: "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?"— 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? 1

2 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 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 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 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 6 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!" 7

8 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 9 Boolean A variable that can have a value of true or false is declared as a boolean. Example… boolean didItWork; diditWork = true;

10 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 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 12 Question Which of these names is a valid variable name? – with – myVariable99 – my%Variable – my_Variable – my_Variable&Helper

13 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 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 15 Assigning Values Use the equals ( = ) sign e.g. myFirstVariable = 101;

16 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 17 Assigning Variables to Variables var myVariable; var myOtherVariable; myOtherVariable = 22; myVariable = myOtherVariable;

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

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

20 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 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 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 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

24 Null Values var nullString = null; var undefString; var blankString = ""; //has a value of empty string 24

25 End of Lesson 25


Download ppt "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?"

Similar presentations


Ads by Google