Editing deck
Primitive Data Types
Immutables (not changeables)
// Primitive Data Types
String // 'Javascript is awesome!', "Really awesome!"
Number // 4, 150, 2345, 4.5
Boolean // true, false, -1, 0, 1,
null // "defined" as no value
undefined // not yet defined (default)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Strings
The data type for plain language
Strings
// Javascript Strings
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
// RULE 1: Must be between balanced quotes
typeof 'Javascript is awesome!'; // 'string'
// RULE 2: Escape special characters
'Can\'t beat Javascript so don\'t try';
// RULE 3: Can have numbers, symbols, ANYTHING
'Jenny\'s #: ($%@)867-5309';
Strings
// Javascript Strings
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
// Indexed, starting at 0
'Dancing in the street'[0]; // "D"
0123456789...
// Immutable
'I hear, the secrets that...'
0123456789...
// Iterable (can loop through)
for (var index = 0; index < 9; index++) {
console.log('JavaScript'[index]);
}
[2] = 'f';
// NOPE!!!
// "J", "a", "v", "a", "S", "c", "r", "i", "p", "t"
// String Methods (helpers)
Strings Methods (helpers)
// How many characters?
'Don\'t sweat the technique'.length // 24
// Convert to uppercase
'dance, magic dance!'.toUpperCase // 'DANCE, MAGIC DANCE'
// Where is the letter
'Borderline!'.indexOf('r'); // 2
// Search for other Javascript string methods
// Use them when possible
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635390/stucked.png)
Strings
- Primitive Javascript Data Type
- Immutable (no editing)
- Characters, symbols, numbers
- Escape special characters
- Must be between balanced quotes
Key Takeaways
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635390/stucked.png)
Strings Exercises!!!
- Find a pair - ("Driver" and "Navigator")
- Set up your work environments
- Slides: Pair Programming
- NAVIGATOR - TELLS DRIVER WHAT TO TYPE
- DRIVER - HANDS ON KEYBOARD
- Switch roles every 10 mins or 3 problems
- Spend 1-2 minutes reviewing code
- Share code with codeshare.io/[Share this!]
Instructions:
"Coding tiiiime"
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635390/stucked.png)
HELP???
Let's code!!!
- Lecture Slides
- w3schools
- stackoverflow
- mdn.io
- TA/Teacher
Numbers
Just do all the stuff you do with numbers...
Numbers
// Javascript Numbers
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
JavaScript Memory
563463456...
2357
45.3346636...
13
// Numbers are numbers
13, 2357, 5634634564356456
// Decimals too
45.33466362363
// Oh! Negative nums
-45324543
-45324543
Numbers
// Javascript Numbers
// Mathmatical operations
> 21 + 7: // ???
> 3.14 + 5: // ???
> 3 * 5; // ???
> 15 / 3 // ???
> 15 % 9; // ???
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
JavaScript Memory
3 * 5
3.14 + 5
15 / 3
21 + 7
15 % 9
Numbers
// Javascript Numbers
// Mathmatical operations
> 21 + 7: // ???
> 3.14 + 5: // ???
> 3 * 5; // ???
> 15 / 3 // ???
> 15 % 9; // ???
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
JavaScript Memory
15
8.4
5
28
6
Numbers Methods(helpers)
// Javascript Numbers' "METHODS"
// Is this a number?
typeof 523454 // ???
// 'STRINGIFY' this number
55.00.toString(); // ???
77.toString(); // ???
(99).toString(); // ???
// Numberify a 'STRINGIFIED'
Number('453'); // ???
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
JavaScript Memory
ERROR
55
"99"
"number"
453
Numbers Methods(helpers)
// Javascript Numbers Methods
// When adding numbers
600 + '400'; // ???
// When using some methods
54.435345.toFixed(2); // ???
// Combining methods and operations
10 + 54.435.toFixed(2); // ???
// Just to be safe???
Number(10 + 54.435.toFixed(2)); // ???
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
JavaScript Memory
"1054.44"
"54.44"
Try it!
"600400"
Be Careful and pay attention to detail!
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
JavaScript Memory
64.44
54.441
1054.44
Numbers Methods (helpers)
// Javascript Numbers Methods
// What's this???
Number(10 + 54.435.toFixed(2));
Be Careful and pay attention to detail!
// And what about this?
Number(54.435.toFixed(2) + 10);
// And this?
Number(54.435345.toFixed(2)) + 10;
// Javascript Numbers Methods
Numbers
- Primitive Javascript Data Type
- Immutable (no editing)
- Do number stuff
- Be careful when using methods
- Be when using with strings
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635390/stucked.png)
Key Takeaways
Numbers
Let's code!!!
[Add link to exercises here and/if directions]
[Anything else?]:
new pairs?
how long?
Resources?
Booleans
True, False | Truthy <> Falsy
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Booleans
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
// No grey areas, evaluates explicitly to true or false
// Absolute Equivalence
> true; // Stevie Wonder is a musical genius
> false; // Is water dry?
// Not explicitly true or false, "If I had to pick one"
// "Truthy" <> Drake is more pop music than anything
> 1, 'string', {one: 1}, [1, 2];
// "Falsy" <> Preston's pants are too colorful
> -1, 0, null, undefined, "", {}, [];
// Relative Equivalence
"Bool"
// Absolute Equivilence
JavaScript Memory
false
true
true
// Is 4 equal to 4
> 4 === 4; // ???
// Is 4 not equal to 5
> 4 !== 5; // ???
// Is 4 greater than 5
> 4 > 5; // ???
"Bool"
// Absolute Equivilence
JavaScript Memory
false
false
true
// Is this the correct data type?
> typeof 'B, BBoys and [B]Girls'
// Is this the correct data type?
> typeof 'Love is a battlefield!'
// Is this the correct data type?
> typeof function letsDance() {}
=== "string";
=== "number";
=== "function";
"Bool"
// Absolute Equivilence
JavaScript Memory
false
> // Write a function that returns true
// if 'Moose rocks dance floors' EVALUATES to true
(function() {
// If the condition fails return false
return false;
})() // ???
// Check if 'Moose rocks dance floors' is EQUAL to TRUE
if ('Moose rocks dance floors' === true) {
JavaScript Memory
false
// if yes, break the function and return true
return true;
}
"Bool"
// Absolute Equivilence
JavaScript Memory
true
> // Create function that asks a user who the 5 goat rappers are
(function() {
var isTheGoat = false;
})();
// Google it!
var goat = prompt("Who are the 5 goat rappers?").toUpperCase();
JavaScript Memory
false
// Check if the 'goat' is the correct answer
if (goat === 'DYLAN') {
// Is the goat?
isTheGoat = true;
// if correct, tell us who
for (var i = 0; i < 5; i++) { alert(goat + '!') };
// Then tell us why
setTimeout(function() {alert('BECAUSE I SPIT HOT FIRE!')}, 2000);
// Break out of the function and tell us the 'truth'
return isTheGoat; // OPTIONAL
}
return isTheGoat; // true or false no matter what
"Bool"
// Relative Equivilence
JavaScript Memory
>// Does Moose rock dance floors?
if ('Moose rocks dance floors!!!') {
} // ???
JavaScript CONSOLE
// If yes, print true
console.log(true);
undefined
true
Booleans
Absolute Equivalence
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635390/stucked.png)
Key Takeaways
- Positive numbers (1, 2, 3)
- "string value"
- [ 'populated', 'arrays' ]
- { populated: 'objects' }
- true | false
Relative Equivalence
- 0, -1 and below
- "" - (empty string)
- [ ] - (empty array)
- { } - (empty objects)
- null, undefined
'falsy':
'truthy':
Operators
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Let's make stuff happen!!!
Operator Types
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Mathematical
- Addition - ( + )
- Subtraction - ( - )
- Multiplication - ( * )
- Division - ( / )
- Modulus - ( % )
- Increment - ( ++ )
- Decrement - ( -- )
- Greater than - ( >, >= )
- Less than - ( <, <= )
Access/Assign
- Brackets - ( [ ] )
- dot - ( . )
- Assignment - ( = )
- Assignment - ( += )
- Assignment - ( -= )
Comparison
- Equality ( === )
- Not equal ( !== )
Logical
- And - ( && )
- Or - ( || )
- Not - ( ! )
Ignore
- ( == )
- ( != )
Other
- Grouping - ( )
- Ternary - ( ? )
Keywords
- var
- function
- for
- in
- typeof
- this
- new
I quit!!!
Operator Types
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Mathematical
- Addition - ( + )
- Modulus - ( % )
Access/Assign
- Brackets - ( [ ] )
- dot - ( . )
- Assignment - ( = )
Comparison
- Equality ( === )
- Not equal ( !== )
Ignore
- ( == )
- ( != )
Keywords
- typeof
Current concerns...
remember,
don't worry about:
Phew!!!
Operator Types
// Mathematical
JavaScript Memory
"I would walk 500 miles..."
"I want to ride my BICYCLE!!!"
9
// Review the slides on numbers if necessary
> 5 + 4; // Self explanatory
> 12 % 4; // Modulus, gets the remainder
// One more thing...
'I want to ride my'
// What about...
'I would walk ' + 500 + ' miles...' //???
8
+ ' BICYCLE!!!'; // ???
String 'concatenation'
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
For more complex operations, read the docs!
Operator Types
// Comparison (equality)
JavaScript Memory
true
// Are these the same? (in terms of ASCII)
> 1000 === 1000; // ???
> 5000 !== 5000; // ???
// And these...?
> 'Basketball' !== 'and Mr. Curtis Blow'; // ???
> 'Sussudio' === 'Sussudio'; // ???
// What about types?
typeof 'Do the Beat Street Strut!'; // ???
typeof { starlight: 'startbright'}; // ???
typeof 8675309; // ???
typeof function() {}; // ???
false
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
true
true
"object"
"string"
"number"
"function"
Again, DO NOT use '!=' or '=='
Operator Types
// Assignment and Access
// 'Assignment Operator' (NOT EQUALS!)
> var planet = 'rock!'; // (see variables)
// Arrays [bracket notation only]
['Don\'t', 'stop', 'the', planet][3]; // ???
['do', 'the', 'conga'][3] = 'beat'; // ???
// Objects
> { }['first'] = 'Gloria'; // ???
> { first: 'Gloria' }.last = 'Estefan'; // ???
> { music: 'is gonna get you!' }['music']; // ???
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Just a quick intro, more on these later.
JavaScript Memory
"rock!"
"rock!"
{ "first":"Gloria" }
['do', 'the', 'conga', 'beat']
"is gonna get you!"
{ "first":"Gloria", "last":"Estefan" }
Key Takeaways
- There is no shortage of operators
- Read the docs for your specific use case
- Know the difference between = and ===
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Operators
Remember:
'==' | '!='
'===' | '!=='
Variables
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Oh wait, I need to use that again
Variables
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
- Declaration and assignment
- Referencing primitive values
- Variables rules
- Good naming conventions
- Bad naming conventions
- Example use cases
What we'll cover:
Variables
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
// Variable declaration
'Stevie Nicks'
'Edge of Seventeen'
001
002
Slot
JavaScript Memory
// Proper Declaration
> var name = 'Stevie Nicks';
> var song = 'Edge of Seventeen';
// Don't use (for now)
window.newerName = 'Charlie Parker';
window.newerSong = 'Early in the Morning';
// NEVER USE
newName = 'Eddie Money';
newSong = 'Take me home tonight';
Bad convention: 'new___'
'declaration'
'assigment'
Variables
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
// Rules and Conventions
// Declare and assign at the top of your scope/code
2 > var favBand = 'Earth Wind and Fire';
3 > var favSong = 'Fantasy';
4
5 > console.log(favBand, favSong);
// Good Examples (declarations only)
> var songsArray; // Descriptive of the reference
> var artistName; // Single word
> var templeOfTheDog; // 'camelCase': hungryHungryHungry
// Words not to use (your code will break):
// > var new, var, in, function, String, Number, break, typeof...
// Basically, If your editor highlights the word, don't use it!
// Bad Examples (declarations only)
> var SongTitles; // Start with lowercase
> var tracknumber; // Use camelCase
> var newArray; // Of what?
> var &no$yb@ls; // Code will break
Use 'console.log' to check values
Variables
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
// Variable declaration
'David'
'Bowie'
001
002
Slot
JavaScript Memory
> firstNameAgain;
> var firstNameAgain = 'David';
// What is Javascript doing?
> firstName = 001;
> lastName = 002;
// Proper Declaration
> var firstHame = 'David';
> var lastName = 'Bowie';
003
'David'
= 003;
Variables
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
// Variable Usage
'StevieWonder'
'Stevie Wonder'
'Stevie'
'Wonder'
// Proper Declaration
> var firstHame = 'Stevie';
> var lastName = 'Wonder';
// We want his full Name
> firstName + lastName;
// 'Stevie' + 'Wonder'
// Fix the formatting
> firstName + ' ' + lastName;
// 'Stevie' + ' ' + 'Wonder'
JavaScript Memory
Variables
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
// Variable Usage
'Stevie Wonder'
'Stevie'
'Wonder'
Reference Error
// Proper Declaration
> var firstHame = 'Stevie'; // 001
> var lastName = 'Wonder'; // 002
// Print the fullName
console.log(fullName); // ???
// Fix the formatting
> firstName + ' ' + lastName; // 003
// 'Stevie' + ' ' + 'Wonder'
001
002
Slot
003
JavaScript Memory
JavaScript Console
Variables
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
// Variable Usage
'Scott Weiland'
'Scott'
JavaScript Memory
undefined
'Scott Weiland'
'Scott'
JavaScript Console
'Scott Weiland'
Makes a copy for 'primitive' types.
// Declare before using
> var fullName = 'Scott Weiland';
> var firstName = fullName.split(' ')[0];
> console.log(fullName); // ???
> console.log(firstName); // ???
34 // What's gonna happen?
35 > console.log(stoneTemplePilots); // ???
36 > var stoneTemplePilots = fullName; // ???
// Overwriting
> fullName = 'Stone Temple Pilots'; // ???
> console.log(fullName); // ???
'Declared' or 'namespaced' but not 'defined' before it's used. Hence 'undefined'
001
002
Slot
003
// 001
// 002
'Stone Temple Pilots'
004
'Stone Temple Pilots'
// 004
// 003
// 001
// 002
// 004
Variables
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
'Radio'
'Head'
JavaScript Memory
'Radio Head'
JavaScript Console
'Radio Head'
How does the interpreter work?
1 // Top of code scope
2 > var firstName; // undefined
3 > var lastName; // undefined
// skip lines 4 - 53
001
002
Slot
003
54 > lastName => 002 // 'Head'
55 > firstName => 001 // 'Radio'
53 // When we do this:
54 > var firstName = 'Radio';
55 > var lastName = 'Head';
56 > console.log(firstName + ' ' + lastName);
// When we do this:
54 > lastName = 'Head'
55 > firstName = 'Radio'
56 > console.log(lastName + ' ' + firstName);
56 > console.log(001 + ' ' + 002); // ???
56 > console.log(firstName + ' ' + lastName);
Create a 'namespace' and'hoist' anything with the 'var' keyword to the top of the scope
Execute the statement depending on the variables.
Then assign them
// 003
Key Takeaways
- Variables allow us to re-use values
- They just point to a value in memory
- Values don't depend on them, we do
- Use good, descriptive names
-
What is an 'undefined' variable?
- Declare variables before using them
- Always use the 'var' keyword when declaring
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Variables
Vocabulary:
- Declaration
- Assignment
- Hoisting
- Namespace
Operator Types
// Mathematical
JavaScript Memory
"I would walk 500 miles..."
"I want to ride my BICYCLE!!!"
9
// Review the slides on numbers if necessary
> 5 + 4; // Self explanatory
> 12 % 4; // Modulus, gets the remainder
// One more thing...
'I want to ride my'
// What about...
'I would walk ' + 500 + ' miles...' //???
8
+ ' BICYCLE!!!'; // ???
String 'concatenation'
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
For more complex operations, read the docs!
Variables
// Assignment and Access
// 'Assignment Operator' (NOT EQUALS!)
> var planet = 'rock!'; // (see variables)
// Arrays [bracket notation only]
['Don\'t', 'stop', 'the', planet][3]; // ???
['do', 'the', 'conga'][3] = 'beat'; // ???
// Objects
> { }['first'] = 'Gloria'; // ???
> { first: 'Gloria' }.last = 'Estefan'; // ???
> { music: 'is gonna get you!' }['music']; // ???
![](https://s3.amazonaws.com/media-p.slid.es/uploads/362520/images/1635394/stucked.png)
Just a quick intro, more on these later.
JavaScript Memory
"rock!"
"rock!"
{ "first":"Gloria" }
['do', 'the', 'conga', 'beat']
"is gonna get you!"
{ "first":"Gloria", "last":"Estefan" }
Editing deck
By telegraphprep
Editing deck
This is an introduction to the primitive Javascript data types
- 804