Chapter 2: What's your type?
-
Upload
seth-mclaughlin -
Category
Software
-
view
284 -
download
2
description
Transcript of Chapter 2: What's your type?
![Page 1: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/1.jpg)
chapter 2
what’s your type?
![Page 2: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/2.jpg)
expression
semicolon ends the expression
arithmetic operator
value value
2 + 2;
![Page 3: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/3.jpg)
2 + 2;
value(number)
value(number)
![Page 4: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/4.jpg)
Numbers
Good for... Not good for...
● A person’s age
● Day of the month
● Time
● Score in a game
● A person’s name
● Day name (Friday, etc.)
● Mailing address
● Chat message
![Page 5: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/5.jpg)
Booleans
A binary value, having two possible values called “true” and “false”
Bool·e·anˈbo͞olēən/
![Page 6: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/6.jpg)
Booleans
var isAwake = true;
![Page 7: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/7.jpg)
Booleans
var isAwake = true;
keyword
![Page 8: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/8.jpg)
Booleans
var isAwake = true;
identifier
![Page 9: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/9.jpg)
Booleans
var isAwake = true;
boolean literal
![Page 10: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/10.jpg)
Booleans
var isAwake = true;
assignment operator
![Page 11: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/11.jpg)
Booleans
var isAwake = true;
keyword identifier
assignment operator
boolean literal
![Page 12: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/12.jpg)
Booleans
var isAwake = true;
var isSad = false;
![Page 13: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/13.jpg)
Booleans
Boolean('Seth'); // evaluates to true
Boolean(''); // evaluates to false
Boolean(0); // evaluates to false
Boolean(234); // evaluates to true
![Page 14: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/14.jpg)
Booleans
Good for...
● Constructing logical expressions
if (isAwake) {
// do
something
}
![Page 15: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/15.jpg)
function add(number1, number2) {
return number1 + number2;
}
Functions
Group code into a reusable chunk.
![Page 16: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/16.jpg)
function add(number1, number2) {
return number1 + number2;
}
Functions
Group code into a reusable chunk.
keyword
![Page 17: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/17.jpg)
Functions
Group code into a reusable chunk.
function add(number1, number2) {
return number1 + number2;
}
function name
![Page 18: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/18.jpg)
Functions
Group code into a reusable chunk.
function add(number1, number2) {
return number1 + number2;
}
argument argument
![Page 19: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/19.jpg)
Functions
Group code into a reusable chunk.
function add(number1, number2) {
return number1 + number2;
}function body
![Page 20: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/20.jpg)
Functions
Group code into a reusable chunk.
function add(number1, number2) {
return number1 + number2;
} keyword
![Page 21: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/21.jpg)
Functions
Group code into a reusable chunk.
function add(number1, number2) {
return number1 + number2;
} value to return
![Page 22: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/22.jpg)
Functions
Group code into a reusable chunk.
function add(number1, number2) {
return number1 + number2;
}
keyword
function name
argument argument
keyword value to return
function body
![Page 23: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/23.jpg)
Functions
function add(number1, number2) {
return number1 + number2;
}
add(10, 55); // evaluates to 65
invoke the function
![Page 24: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/24.jpg)
Functions
Good for...
● Organizing your code into reusable pieces
● Holding code to draw a character in a game
● Calculating some value
![Page 25: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/25.jpg)
Objects
Store a group of values.
var player = {
score: 100,
level: 3
};
![Page 26: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/26.jpg)
Objects
Store a group of values.
var player = {
score: 100,
level: 3
};key
value
![Page 27: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/27.jpg)
Objects
// bracket notation
player['score']; // evaluates to 100
// dot notation
player.score; // evaluates to 100
![Page 28: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/28.jpg)
Objects
var player = {
score: 100,
level: 3
};
player.isAlive = true;
player.isAlive; // evaluates to true
![Page 29: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/29.jpg)
Objects
var player = {
score: 100,
level: 3
};
player.score = player.score + 50;
player.score; // evaluates to 150
![Page 30: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/30.jpg)
Objects
player.totalScore = function () {
var total = this.score * this.level;
};
player.totalScore(); // evaluates to 450 (150 * 3)
![Page 31: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/31.jpg)
Built-in Objects
Math.random(); // evaluates to a random number,
// between 0 and 1
console.log('Hello World'); // prints string to
// developer console
![Page 32: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/32.jpg)
Objects
Good for...
● Storing player status in a game
● Grouping behavior with data
![Page 33: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/33.jpg)
Strings
Store alphanumeric values: letters, words, sentences.
Examples:
Hello, my name is Bob.SethtSeth23
![Page 34: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/34.jpg)
Strings
var foo = "abc"; // correct
var bar = 'abc'; // correct
var biz = 'abc"; // incorrect
Quoting styles.
![Page 35: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/35.jpg)
Strings
'Seth' + ' ' + 'McLaughlin'; // evaluates to
// 'Seth McLaughlin'
Adding strings (concatenation).
![Page 36: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/36.jpg)
Strings
'Seth' + 77; // evaluates to 'Seth 77'
Adding strings to numbers.
![Page 37: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/37.jpg)
Strings
'Seth' - 'McLaughlin'; // evaluates to NaN
'Seth' / 'McLaughlin'; // evaluates to NaN
'Seth' * 'McLaughlin'; // evaluates to NaN
-, /, * operators do not apply to string values.
NaN means “Not A Number” (invalid value)
![Page 38: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/38.jpg)
Strings
'Seth'.length; // evaluates to the number 4
'Seth'[1]; // evaluates to the string 'e'
'Seth'.indexOf('e'); // evaluates to the number 1
'Seth'.toUpperCase(); // evaluates to the string 'SETH'
'Seth'.toLowerCase(); // evaluates to the string 'seth'
Some of the built-in properties.
![Page 39: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/39.jpg)
Strings
Good for...
● Storing text● A player’s name● A website URL● A chat message
![Page 40: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/40.jpg)
Arrays
var friends = ['Richard', 'Tom', 'Susie'];
Store a list of items.
Richard Tom Susie
![Page 41: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/41.jpg)
Arrays
push() -- add to end
Richard Tom Susie
![Page 42: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/42.jpg)
Arrays
push() -- add to end
Richard Tom Susie
friends.push('Sara');
![Page 43: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/43.jpg)
Arrays
push() -- add to end
Richard Tom Susie
friends.push('Sara');
Sara
![Page 44: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/44.jpg)
Arrays
unshift() -- add to beginning
Richard Tom Susie
![Page 45: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/45.jpg)
Arrays
unshift() -- add to beginning
Richard Tom Susie
friends.unshift('Sara');
![Page 46: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/46.jpg)
Arrays
unshift() -- add to beginning
Richard Tom Susie
friends.unshift('Sara');
![Page 47: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/47.jpg)
Arrays
unshift() -- add to beginning
Richard Tom Susie
friends.unshift('Sara');
Sara
![Page 48: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/48.jpg)
Arrays
count the items
Richard Tom Susie
friends.length; // evaluates to 4
Sara
![Page 49: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/49.jpg)
Arrays
Get an item
Richard Tom Susie
friends[2]; // evaluates to 'Tom'
friends[0]; // evaluates to 'Sara'
Sara
![Page 50: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/50.jpg)
null and undefined
Special values to indicate a lack of value
var foo;
foo; // evaluates to undefined
var bar = null;
bar; // evaluates to null
![Page 51: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/51.jpg)
null and undefined
function myFunction() {
var age = 34 + 10;
}
myFunction(); // evaluates to undefined since the
// function does not return a value
![Page 52: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/52.jpg)
null and undefined
Boolean(undefined); // evaluates to false
Boolean(null); // evaluates to false
![Page 53: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/53.jpg)
Review
![Page 54: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/54.jpg)
What does concatenation mean?
![Page 55: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/55.jpg)
What is the boolean value of 2 + '2';?
![Page 56: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/56.jpg)
Write a function to multiply two numbers together,
and return the result.
![Page 57: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/57.jpg)
Create an array to hold a list of state names. How can
you get the number of states in your list?
![Page 58: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/58.jpg)
Create an object to represent a person. This object
should have two properties:
● name - a string
● sayHello - a function
When the function sayHello is invoked, the person’s
name should be printed to the browser’s console.
![Page 59: Chapter 2: What's your type?](https://reader034.fdocuments.in/reader034/viewer/2022042700/554f9fd0b4c9057b298b483c/html5/thumbnails/59.jpg)
Intro to Programmingwith Seth McLaughlin