INTERACTIVITY
01.04 Variables WITH JAVASCRIPT
Variables
INTERACTIVITY
01.04 Variables WITH JAVASCRIPT
Storing Data
• Part of learning to program is learning to store
data
• In JavaScript, data is stored in variables
• To use a variable, you have to declare it
var name;
keyword variable name
INTERACTIVITY
01.04 Variables WITH JAVASCRIPT
Storing Data
Variable Name Memory Location Value “stored” in computer
name 11001100001101 “Christopher”
age 11001100001110
… 11001100001111
… 11001100010000
… 11001100010001
… 11001100010010
… 11001100010011
INTERACTIVITY
01.04 Variables WITH JAVASCRIPT
Variable Names
• Consists of letters, digits, underscores, and
dollar sign($)
• Can not start with a digit
• Are case-sensitive…
• name Name, naMe, NAME are all different variables
• Should be mnemonic (meaningful)
INTERACTIVITY
01.04 Variables WITH JAVASCRIPT
Variable assignments
• It is silly to have a variable if you are
never going to use it
• You can assign values using the =
operator
var name =“Colleen” ;
assignment operator
INTERACTIVITY
01.04 Variables WITH JAVASCRIPT
Assignment statements
• I like to refer to the LHS and RHS of statements
• LHS – the variable being updated
• RHS – the new value that will be stored in the
variable
var name;NO!!
“Colleen” = name;;
name =“Colleen”
LHS RHS
INTERACTIVITY
01.04 Variables WITH JAVASCRIPT
Using a Variable
var name = prompt(“What is your name?”);
[Link](name);
var date= Date();
[Link](date);
var location= [Link];
[Link](location);
INTERACTIVITY
01.04 Variables WITH JAVASCRIPT
Review
• Variables are a key component of creating
interactive programs
• We will be using them in the remaining
lectures so practice them and feel
comfortable
INTERACTIVITY
01.04 Variables WITH JAVASCRIPT
Acknowledgements/Contributions
These slides are Copyright 2015- Colleen van Lent as part of
[Link] and made available under a Creative
Commons Attribution Non-Commercial 4.0 License. Please maintain
this last slide in all copies of the document to comply with the attribution
requirements of the license. If you make a change, feel free to add
your name and organization to the list of contributors on this page as
you republish the materials.
Initial Development: Colleen van Lent , University of Michigan School of
Information