chapter 2: what's your type?

59
chapter 2 what’s your type?

Upload: seth-mclaughlin

Post on 11-May-2015

282 views

Category:

Software


2 download

DESCRIPTION

Introduction to Programming course, Chapter 2. Learn about the data types in JavaScript and how to use them. http://i2p.sethmcl.com

TRANSCRIPT

Page 1: Chapter 2: What's your type?

chapter 2

what’s your type?

Page 2: Chapter 2: What's your type?

expression

semicolon ends the expression

arithmetic operator

value value

2 + 2;

Page 3: Chapter 2: What's your type?

2 + 2;

value(number)

value(number)

Page 4: Chapter 2: What's your type?

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?

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?

Booleans

var isAwake = true;

Page 7: Chapter 2: What's your type?

Booleans

var isAwake = true;

keyword

Page 8: Chapter 2: What's your type?

Booleans

var isAwake = true;

identifier

Page 9: Chapter 2: What's your type?

Booleans

var isAwake = true;

boolean literal

Page 10: Chapter 2: What's your type?

Booleans

var isAwake = true;

assignment operator

Page 11: Chapter 2: What's your type?

Booleans

var isAwake = true;

keyword identifier

assignment operator

boolean literal

Page 12: Chapter 2: What's your type?

Booleans

var isAwake = true;

var isSad = false;

Page 13: Chapter 2: What's your type?

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?

Booleans

Good for...

● Constructing logical expressions

if (isAwake) {

// do

something

}

Page 15: Chapter 2: What's your type?

function add(number1, number2) {

return number1 + number2;

}

Functions

Group code into a reusable chunk.

Page 16: Chapter 2: What's your type?

function add(number1, number2) {

return number1 + number2;

}

Functions

Group code into a reusable chunk.

keyword

Page 17: Chapter 2: What's your type?

Functions

Group code into a reusable chunk.

function add(number1, number2) {

return number1 + number2;

}

function name

Page 18: Chapter 2: What's your type?

Functions

Group code into a reusable chunk.

function add(number1, number2) {

return number1 + number2;

}

argument argument

Page 19: Chapter 2: What's your type?

Functions

Group code into a reusable chunk.

function add(number1, number2) {

return number1 + number2;

}function body

Page 20: Chapter 2: What's your type?

Functions

Group code into a reusable chunk.

function add(number1, number2) {

return number1 + number2;

} keyword

Page 21: Chapter 2: What's your type?

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?

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?

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?

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?

Objects

Store a group of values.

var player = {

score: 100,

level: 3

};

Page 26: Chapter 2: What's your type?

Objects

Store a group of values.

var player = {

score: 100,

level: 3

};key

value

Page 27: Chapter 2: What's your type?

Objects

// bracket notation

player['score']; // evaluates to 100

// dot notation

player.score; // evaluates to 100

Page 28: Chapter 2: What's your type?

Objects

var player = {

score: 100,

level: 3

};

player.isAlive = true;

player.isAlive; // evaluates to true

Page 29: Chapter 2: What's your type?

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?

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?

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?

Objects

Good for...

● Storing player status in a game

● Grouping behavior with data

Page 33: Chapter 2: What's your type?

Strings

Store alphanumeric values: letters, words, sentences.

Examples:

Hello, my name is Bob.SethtSeth23

Page 34: Chapter 2: What's your type?

Strings

var foo = "abc"; // correct

var bar = 'abc'; // correct

var biz = 'abc"; // incorrect

Quoting styles.

Page 35: Chapter 2: What's your type?

Strings

'Seth' + ' ' + 'McLaughlin'; // evaluates to

// 'Seth McLaughlin'

Adding strings (concatenation).

Page 36: Chapter 2: What's your type?

Strings

'Seth' + 77; // evaluates to 'Seth 77'

Adding strings to numbers.

Page 37: Chapter 2: What's your type?

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?

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?

Strings

Good for...

● Storing text● A player’s name● A website URL● A chat message

Page 40: Chapter 2: What's your type?

Arrays

var friends = ['Richard', 'Tom', 'Susie'];

Store a list of items.

Richard Tom Susie

Page 41: Chapter 2: What's your type?

Arrays

push() -- add to end

Richard Tom Susie

Page 42: Chapter 2: What's your type?

Arrays

push() -- add to end

Richard Tom Susie

friends.push('Sara');

Page 43: Chapter 2: What's your type?

Arrays

push() -- add to end

Richard Tom Susie

friends.push('Sara');

Sara

Page 44: Chapter 2: What's your type?

Arrays

unshift() -- add to beginning

Richard Tom Susie

Page 45: Chapter 2: What's your type?

Arrays

unshift() -- add to beginning

Richard Tom Susie

friends.unshift('Sara');

Page 46: Chapter 2: What's your type?

Arrays

unshift() -- add to beginning

Richard Tom Susie

friends.unshift('Sara');

Page 47: Chapter 2: What's your type?

Arrays

unshift() -- add to beginning

Richard Tom Susie

friends.unshift('Sara');

Sara

Page 48: Chapter 2: What's your type?

Arrays

count the items

Richard Tom Susie

friends.length; // evaluates to 4

Sara

Page 49: Chapter 2: What's your type?

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?

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?

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?

null and undefined

Boolean(undefined); // evaluates to false

Boolean(null); // evaluates to false

Page 53: Chapter 2: What's your type?

Review

Page 54: Chapter 2: What's your type?

What does concatenation mean?

Page 55: Chapter 2: What's your type?

What is the boolean value of 2 + '2';?

Page 56: Chapter 2: What's your type?

Write a function to multiply two numbers together,

and return the result.

Page 57: Chapter 2: What's your type?

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?

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?

Intro to Programmingwith Seth McLaughlin