Data Types and Variables
Javascript has 3 basic types of data
- numbers
- strings
- booleans
Let's Practice!!
Numbers and operators
JavaScript lets you perform basic mathematical operations like addition, subtraction, multiplication, and division. To make these calculations, we use the symbols +, -, *, and /, which are called operators.
Variables
Information or data that changes
var name = "Kenneth";
var myFavoritePokemon = "bulbasaur";
myFavoritePokemon = "Charmander";
var myFavoriteNumber = 12;
var wearsGlasses = true;
Rules for naming Variables
- Variables are Case sensitive
- Must begin with letters, $ or _
- Only contain letters, numbers, $ and _
- Avoid reserved words(<=click to view)
- Make it meaningful
- Use camelCase convention for multipleWords
OK:
var myPokemon, $myPokemon, _charmander, _Charmander;
Not OK:
var 3MasterBalls, iBeatTheGymLeader!
Let's Practice
Excercise 1
How do we write my favorite pokemon in camel case?
Exercise/Problem solving
Find how many seconds is in an hour using variables
*Hint*
seconds in a minute is 60 seconds
minutes in an hour is 60 minutes
Now find how many seconds is in 1 hour
Exercise 2 Answer
var secondsInAMinute = 60;
var minutesInAnHour = 60;
var secondsInAnHour = secondsInAMinute * minutesInAnHour;
secondsInAnHour;
//The answer is 3600
Exercise 3
Find your age in 10 years
Exercise 3 Anwser
Find your age in 10 years
var age = 10;
var inTenYears = 10;
var myFutureAge = age + inTenYears;
console.log(myFutureAge)
//Answer is 20
Working With Strings
Strings are written within quotes either “ “ double or ‘ ‘ single
var myFavoriteShow = "Pokemon";
var myFavoriteFood = 'chicken';
Joining Strings
var first = “ken”
var last = “banico”
var fullName = first + last
//output will be "kenbanico"
Finding Length of a String
"Supercalifragilisticexpialidocious".length;
34
Getting a Single Character from a String
var myName = "Ken";
myName[0];
//"K"
myName[1];
//"e"
myName[2];
//“n"
Excercise Time
Find The Secret Code
Get the 2nd character of each word and print out the secret code
Hint
var myName = "ken";
myName[0];
"k"
var codeWord1 = “igloo”;
var codeWord2 = “buddy”;
var codeWord3 = “cat”;
var codeWord4 = “umbrella”;
Answer
var codeWord1 = "igloo";
var codeWord2 = "buddy";
var codeWord3 = "cat";
var codeWord4 = "umbrella";
var theSecret = codeWord1[1]+codeWord2[1]+codeWord3[1]+codeWord4[1];
console.log(theSecret);
Cutting Strings
Example
var myLongString = "Gotta Catch Em All";
myLongString.slice(6,9);
//Type it out and see what the answer is
Cutting Strings Explaination
var myLongString = "Gotta Catch Em All";
myLongString.slice(6,9);
Start
End
G o t t a
0 1 2 3 4 6 7 8 9 10 12 13 15 16 17
C a t c h
E M
A L L
Spaces are counted
and does not include the last number you specify
Not Included
Try it out with your own String
Change Strings To All Capital Letters
"Hello there my name is Professer Oak!".toUpperCase();
Example
Change Strings To All LowerCase Letters
"hElLo ThERE I liKE POKEMoN".toLowerCase();
Example
HARD MODE EXCERCISE
See if you can figure out how to make
"hElLo ThERE I liKE POKEMoN"
into
"Hello there i like pokemon"
HARD MODE EXCERCISE ANSWER
var complicatedString = "hElLo ThERE I liKE POKEMoN";
//this will get the first firstLetter
var firstLetter = complicatedString[0];
//this will make the firstLetter uppercase
firstLetter = firstLetter.toUpperCase();
//this will get the second letter all the way up to the last letter
var restOfTheString = complicatedString.slice(1)
//this will make the rest of the string toLowerCase
restOfTheString = restOfTheString.toLowerCase();
//we combine the two variables to complete the string
console.log(firstLetter+restOfTheString)
data types
By Kenneth Banico
data types
- 254