Download presentation
Presentation is loading. Please wait.
1
Variables and Data Types
JavaScript Variables and Data Types
2
WiLL CONNECT BACK TO HTML NEXT WEEK
OUTPUT WITHOUT ALERT Console.log(); WiLL CONNECT BACK TO HTML NEXT WEEK
3
Holding information
4
Holding information Computer doesn’t understand values
It knows about locations: “cubby holes” Takes locations and does what you tell it: read, write, add
5
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
6
Defining CUBBY HOLES They are called variables Define them
Give them values Take values from them
7
= means “takes the value of” A = B + C; A takes the value of B+C
8
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
9
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
10
VALUES
11
What values can it take? Any “expression” What is an expression?
Variables Constants Operators functions Different for numbers and strings
12
Numbers Regular math operators Variables Constants Functions
13
Random Numbers
14
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!
15
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!
16
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.”
17
What if you don’t want 0-1? Change range? Multiply Not start at 0? Add
Integers only? Round Floor Ceiling
18
Strings Think of Scrabble tiles Each tile is a Letter Number
Punctuation mark Space (blank)
19
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
20
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”
21
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
22
Referencing characters
Each character has a position Note that it starts at 0, not 1 H I M O 1 2 3 4 5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.