Variables and Data Types JavaScript Variables and Data Types
WiLL CONNECT BACK TO HTML NEXT WEEK OUTPUT WITHOUT ALERT Console.log(); WiLL CONNECT BACK TO HTML NEXT WEEK
Holding information
Holding information Computer doesn’t understand values It knows about locations: “cubby holes” Takes locations and does what you tell it: read, write, add
A = B + C; example Cubby Hole 2: add this value Cubby Hole 3: place it here A = B + C; Cubby Hole 1: take this value
Defining CUBBY HOLES They are called variables Define them Give them values Take values from them
= means “takes the value of” A = B + C; A takes the value of B+C
Changing Information A = A + C; Cubby Hole 2: add this value Cubby Hole 1: replace the old value A = A + C; Cubby Hole 1: take this value
To DEFINE var anyName; Defines a variable called anyName Case-sensitive Can be any type of value var aNumber = 1; aNumber is a number Starts with a value of 1
VALUES
What values can it take? Any “expression” What is an expression? Variables Constants Operators functions Different for numbers and strings
Numbers Regular math operators Variables Constants Functions
Random Numbers
Why random numbers? Variability Different results Examples Display one of ten pictures Display one of five quotations Play a game of chance All built on a random number!
Can computers really create random numbers? Computers do as they are told So how can they do something random? They can’t! But we can tell them to find a number that changes often! Needs to not have an easily recognized pattern Pseudo-random Number Generator Options? Time!
Getting a random number Math.random() Returns a value between 0 and 1 One of a series of mathematical functions members of a collection called Math each are prefixed by “Math.”
What if you don’t want 0-1? Change range? Multiply Not start at 0? Add Integers only? Round Floor Ceiling
Strings Think of Scrabble tiles Each tile is a Letter Number Punctuation mark Space (blank)
What is a String? A literal string must be inside double quotes or single quotes “this is a string” and this is not a string. Remember to beware cut and paste
Concatenating (formally) It means combining two or more things into one thing + Anything can be concatenated “awe” & “some” = “awesome” Whitespace only matters inside quotes “a”+“b” same as “a” + “b” “a ” + “b” NOT the same as “a” + “b”
Substring A substring is also a String A substring is a part of another string “cake” is a substring of “birthday cake” so are “day”, “thd”, and “y cake” “they” is not, neither is “hello” or “dude” Will come back to how to extract substrings
Referencing characters Each character has a position Note that it starts at 0, not 1 H I M O 1 2 3 4 5