chapter 2: what's your type?
DESCRIPTION
Introduction to Programming course, Chapter 2. Learn about the data types in JavaScript and how to use them. http://i2p.sethmcl.comTRANSCRIPT
chapter 2
what’s your type?
expression
semicolon ends the expression
arithmetic operator
value value
2 + 2;
2 + 2;
value(number)
value(number)
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
Booleans
A binary value, having two possible values called “true” and “false”
Bool·e·anˈbo͞olēən/
Booleans
var isAwake = true;
Booleans
var isAwake = true;
keyword
Booleans
var isAwake = true;
identifier
Booleans
var isAwake = true;
boolean literal
Booleans
var isAwake = true;
assignment operator
Booleans
var isAwake = true;
keyword identifier
assignment operator
boolean literal
Booleans
var isAwake = true;
var isSad = false;
Booleans
Boolean('Seth'); // evaluates to true
Boolean(''); // evaluates to false
Boolean(0); // evaluates to false
Boolean(234); // evaluates to true
Booleans
Good for...
● Constructing logical expressions
if (isAwake) {
// do
something
}
function add(number1, number2) {
return number1 + number2;
}
Functions
Group code into a reusable chunk.
function add(number1, number2) {
return number1 + number2;
}
Functions
Group code into a reusable chunk.
keyword
Functions
Group code into a reusable chunk.
function add(number1, number2) {
return number1 + number2;
}
function name
Functions
Group code into a reusable chunk.
function add(number1, number2) {
return number1 + number2;
}
argument argument
Functions
Group code into a reusable chunk.
function add(number1, number2) {
return number1 + number2;
}function body
Functions
Group code into a reusable chunk.
function add(number1, number2) {
return number1 + number2;
} keyword
Functions
Group code into a reusable chunk.
function add(number1, number2) {
return number1 + number2;
} value to return
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
Functions
function add(number1, number2) {
return number1 + number2;
}
add(10, 55); // evaluates to 65
invoke the function
Functions
Good for...
● Organizing your code into reusable pieces
● Holding code to draw a character in a game
● Calculating some value
Objects
Store a group of values.
var player = {
score: 100,
level: 3
};
Objects
Store a group of values.
var player = {
score: 100,
level: 3
};key
value
Objects
// bracket notation
player['score']; // evaluates to 100
// dot notation
player.score; // evaluates to 100
Objects
var player = {
score: 100,
level: 3
};
player.isAlive = true;
player.isAlive; // evaluates to true
Objects
var player = {
score: 100,
level: 3
};
player.score = player.score + 50;
player.score; // evaluates to 150
Objects
player.totalScore = function () {
var total = this.score * this.level;
};
player.totalScore(); // evaluates to 450 (150 * 3)
Built-in Objects
Math.random(); // evaluates to a random number,
// between 0 and 1
console.log('Hello World'); // prints string to
// developer console
Objects
Good for...
● Storing player status in a game
● Grouping behavior with data
Strings
Store alphanumeric values: letters, words, sentences.
Examples:
Hello, my name is Bob.SethtSeth23
Strings
var foo = "abc"; // correct
var bar = 'abc'; // correct
var biz = 'abc"; // incorrect
Quoting styles.
Strings
'Seth' + ' ' + 'McLaughlin'; // evaluates to
// 'Seth McLaughlin'
Adding strings (concatenation).
Strings
'Seth' + 77; // evaluates to 'Seth 77'
Adding strings to numbers.
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)
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.
Strings
Good for...
● Storing text● A player’s name● A website URL● A chat message
Arrays
var friends = ['Richard', 'Tom', 'Susie'];
Store a list of items.
Richard Tom Susie
Arrays
push() -- add to end
Richard Tom Susie
Arrays
push() -- add to end
Richard Tom Susie
friends.push('Sara');
Arrays
push() -- add to end
Richard Tom Susie
friends.push('Sara');
Sara
Arrays
unshift() -- add to beginning
Richard Tom Susie
Arrays
unshift() -- add to beginning
Richard Tom Susie
friends.unshift('Sara');
Arrays
unshift() -- add to beginning
Richard Tom Susie
friends.unshift('Sara');
Arrays
unshift() -- add to beginning
Richard Tom Susie
friends.unshift('Sara');
Sara
Arrays
count the items
Richard Tom Susie
friends.length; // evaluates to 4
Sara
Arrays
Get an item
Richard Tom Susie
friends[2]; // evaluates to 'Tom'
friends[0]; // evaluates to 'Sara'
Sara
null and undefined
Special values to indicate a lack of value
var foo;
foo; // evaluates to undefined
var bar = null;
bar; // evaluates to null
null and undefined
function myFunction() {
var age = 34 + 10;
}
myFunction(); // evaluates to undefined since the
// function does not return a value
null and undefined
Boolean(undefined); // evaluates to false
Boolean(null); // evaluates to false
Review
What does concatenation mean?
What is the boolean value of 2 + '2';?
Write a function to multiply two numbers together,
and return the result.
Create an array to hold a list of state names. How can
you get the number of states in your list?
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.
Intro to Programmingwith Seth McLaughlin