http://bit.ly/gdipgh-js-materials
This includes the slides.
You can also access the slides at http://bit.ly/gdipgh-js-slides
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
Tell us about yourself.
How your computer accesses websites
JavaScript is "client side"
Your browser understands it!
Use JavaScript to add content and interactivity
to a web site
The project can be found in the activity
folder.
Open index.html
.
Our activities throughout the class will
build on each other. Make sure
you keep your work.
Each line in JavaScript is an instruction or a script.
When the browser reads it, it "executes the script".
alert('Hello World!');
Statements usually end with a semicolon
Try it out using the console.
alert('Hello World!');
alert displays a message in an alert box
Useful for notifying your user about an important event.
Use sparingly!
Let's try another one!
console.log('Hello World!');
console.log outputs a message to the web console
This is very useful for debugging
Three main ways, just like CSS
<html>
<head>
<title>My Site!</title>
</head>
<body>
My site!
<a href="#" onclick="alert('Hello World!');">click me</a>
</body>
</html>
We'll talk more about this one later
<html>
<head>
<title>My Site!</title>
<script>
alert("Hello World!");
</script>
</head>
<body>
My site!
</body>
</html>
<html>
<head>
<title>My Site!</title>
<script src="scripts.js"></script>
</head>
<body>
My site!
</body>
</html>
scripts.js
alert("Hello World!");
This is usually the best way to add JavaScript.
index.html
<!DOCTYPE html>
<html>
<head>
<title>Jane Doe's Site!</title>
<link rel="stylesheet" href="style.css">
<script src="scripts.js"></script>
</head>
<body>
<img class="photo" src="photo.png">
<h1>Jane Doe</h1>
<p>Hi! My name is Jane Doe.</p>
</body>
</html>
scripts.js
alert("Hello World!");
or
console.log("Hello World");
Are you done or really stuck?
Check out our example.
Information (data) in JavaScript
comes in different types.
A group of characters in quotes
You can use double quotes
console.log("Hello World");
or single quotes
console.log('Hello World!');
Integers
console.log(2012);
Decimals
console.log(3.14);
true or false
console.log(true);
console.log(false);
true/false must be all lowercase
A special keyword denoting an empty (null) value.
console.log(null);
null must be all lowercase
Variables store information
Declare a variable (Give it a name)
var bananas;
Initialize variable (Give it a value)
bananas = 5;
Declare and initialize at the same time!
var bananas = 5;
Change value of variable
bananas = 4;
(I ate a banana)
Begin with a letter, _, or $
Contain letters, numbers, _ and $
var hello;
var _hello;
var $hello;
var hello2;
Names are case sensitive
var hello;
var Hello;
var heLLO;
Most people use either
Snake case
var hello;
var hello_world;
var first_name;
Camel case
var hello;
var helloWorld;
var firstName;
Stick with one for consistency
Give variables meaningful names
bad names
var foo = 3.14;
var fn = "Julie";
var a = true;
good names
var pi = 3.14;
var firstName = "Julie";
var isAllowed = true;
no value yet
var favoriteDinosaur;
You don't know the data type of a variable until you assign it.
The type can change when it is assigned different values.
var looseType; // I am undefined
looseType = 10; // I am a number
looseType = "hi!"; // I am a string
looseType = false; // I am a boolean
looseType = null; // I am null
Used to add hints, notes, suggestions, or warnings.
The browser will ignore them.
// comment on one line
/* comment on
multiple lines
*/
Warning: Other people can see them in the source.
// My super-secret password is password1234
/* I hate my users so much!
I curse them!
*/
Math-y expressions!
var bananas = 5;
var oranges = 2;
var fruit = bananas + oranges;
var bananas = 5;
var people = 3;
var leftover_fruit = bananas - people;
var seconds_per_minute = 60;
var minutes_per_hour = 60;
var seconds_per_hour = seconds_per_minute * minutes_per_hour;
var days_per_year = 365;
var days_per_week = 7;
var weeks_per_year = days_per_year / days_per_week;
Returns the integer remainder of
dividing the two operands.
var grapes = 37;
var people = 7;
var leftover = grapes % people;
Increments the value by 1
Postfix - returns the value before adding one.
var current_hour = 8;
current_hour++; // Returns 8
current_hour; // Returns 9
Prefix - returns the value after adding one.
var current_hour = 8;
++current_hour; // Returns 9
current_hour; // Returns 9
Decrease the value by 1
Postfix - returns the value before subtracting one.
var current_hour = 8;
current_hour--; // Returns 8
current_hour; // Returns 7
Prefix - returns the value after subtracting one.
var current_hour = 8;
--current_hour; // Returns 7
current_hour; // Returns 7
Word-y expressions!
Combines strings together
var phrase = "My name is ";
var name = "Pat";
var sentence = phrase + name + ".";
Assign and concatenate (+=)
var sentence = "My name is ";
sentence += "Jo";
sentence += " and I like " + "cats";
We also used + for addition.
What happens when we mix numbers and strings?
var num = 5; // This is a number
var str = "10"; // This is a string
num + num; // 10
str + str; // "1010"
num + str; // ????
str + num; // ????
var num = 5; // This is a number
var str = "10"; // This is a string
num + num; // 10
str + str; // "1010"
num + str; // "510"
str + num; // "105"
Be careful when you mix data types.
You can get unexpected results.
How many times do you do something in a year?
Are you done or really stuck?
Check out our example.
Runs some JavaScript based on conditions
if (conditions) {
// statement to execute
}
If the conditions evaluate as true,
the JavaScript inside the braces runs.
var bananas = 1;
if (bananas < 2) {
console.log("You should buy more bananas!");
}
What would happen if bananas was set to 3?
A group of statements deliniated by braces.
{
console.log("statement 1");
console.log("statement 2");
console.log("statement 3");
}
Compare the values and return a logical value (boolean) based on whether the comparison is true
Frequently used in conditionals like if
(bananas < 2)
Returns true if the values are equal
1 == 1; // true
1 == 2; // false
"this" == "this" // true
"this" == "that" // false
10 == "10" // ???
Use strict equals to check the data type (recommended)
1 === 1; // true
1 === 2; // false
"this" === "this" // true
"this" === "that" // false
10 === "10" // false
Returns true if the values are equal
Don't confuse = (assign a value)
with == or === (compare a value)
Returns true if the values are not equal
1 != 1; // false
1 != 2; // true
"this" != "this" // false
"this" != "that" // true
10 != "10" // false
Use strict not equals to check the data type (recommended)
1 !== 1; // false
1 !== 2; // true
"this" !== "this" // false
"this" !== "that" // true
10 !== "10" // true
3 > 1; // true
2 > 4; // false
3 >= 1; // true
3 >= 3; // true
2 >= 4; // false
1 < 3; // true
4 < 2; // false
1 <= 3; // true
3 <= 3; // true
4 <= 2; // false
Combine or modify boolean values.
Returns true if all values are true.
Otherwise it returns false.
true && true; // true
true && false; // false
false && true; // false
false && false; // false
true && true && false; // ???
(1 === 1) && ("this" != "that"); // ???
Returns true if any value is true.
Returns false if all values are false.
true || true; // true
true || false; // true
false || true; // true
false || false; // false
false || false || true; // ???
(3 == 4) || (true); // ???
Inverts the boolean value.
!false; // true
!true; // false
!(1 === 1); // ???
!(6 > 2); // ???
var bananas = 5;
var oranges = 2;
if (bananas > 3 && oranges > 3){
console.log('Eat fruit!');
}
if (bananas < 2 || oranges < 2){
console.log('Buy fruit!');
}
if (!(bananas >= 0)){
console.log('How do you have negative bananas?');
}
false isn't the only one
false || null || 0 || "" || '' || undefined; // false
You can use else to perform an alternative action if the "if" fails
var bananas = 5;
if (bananas > 3){
console.log('Eat a banana!');
} else {
console.log('Buy a banana!');
}
You can use else if to have multiple choices
var age = 20;
if (age >= 35) {
console.log('You can vote AND hold any place in government!');
} else if (age >= 25) {
console.log('You can vote AND run for the Senate!');
} else if (age >= 18) {
console.log('You can vote!');
} else {
console.log('You have no voice in government (yet)!');
}
Output a welcome message
Output a welcome message
Are you done or really stuck?
Check out our example.
Get the current date and time
var date = new Date();
Set a specific date and time using a string
var adaLovelaceBday = new Date("December 10, 1815");
var dueDate = new Date("May 1, 2015 12:30");
Set a specific date and time using numbers
var newYear = new Date(2015, 0, 1, 0, 0, 0);
Get information about a date
var date = new Date();
date.getFullYear(); // Year
date.getMonth(); // Month
date.getDate(); // Day
date.getHours(); // Hour
date.getMinutes(); // Minute
date.getSeconds(); // Second
See the Mozilla Developer Network
for
more information about dates
Modify your welcome message to automate
the current month/day
new Date()
.getMonth()
.getDate()
Are you done or really stuck?
Check out our example.