Lauren Herda
Lauren is an Interactive Art Director at @ Mindgrub. Formerly @ Figure 53, Chalk & Chisel, and MICA
I design software and other cool stuff at Figure 53.
www.herda.me
@herda on App.net
@lrenhrda on Twitter
I'm a user experience (UX) and front-end visual designer for Web, mobile, and installation applications.
(Or, at least so far as we're concerned)…
However, JavaScript has uses beyond what we're going to cover in this class, and you in can in fact carry your knowledge of JavaScript over to doing server-side coding!
(These are personal favorites, but there's no shortage of alternatives to suit your tastes.
View → Developer → JavaScript Console
"Lauren";
"Lauren"
5 + 7;
12
12 * 12;
144
console.log("Lauren");
"Lauren"
confirm("Are you there?");
prompt("What is your name?");
In JavaScript, you can sometimes think of things as "nouns" and "verbs."
The "nouns" in JavaScript are your data. Data can be stored in variables.
var myName = "Lauren";
var myNameLength = "Lauren".length;
Now we’ve stored a couple of things in variables: my name, and the length of my name. Let's interact with that data…
alert(myName);
alert(myNameLength);
There are 5 basic data types that you can work with in JavaScript…
booger;
ReferenceError: booger is not defined
booger = null;
booger;
null
5;
5
12.5 * 11;
137.5
var baz = false;
baz;
false
"Lauren";
"Lauren"
|
|
var blah = "Lauren".length != 3;
console.log(blah);
true
blah == 7;
false
Now that we have data and comparators, we can start to make use of them using control structures.
if (
<condition>) {
<code> }
while (
<condition>) {
<code> }
do {
<code> } while (
<condition>)
switch (<condition>) {
case <condition>:
…
break;
default:
…
break;
}
for (<init>; <condition>; <incrementor>) { <code> }
(4 * 2) - (3 + 2);
3
(12 / 3) * 8;
32
75 % 8
3
if (!myVar === false) { … }
if (x == 3 || y == 7) { … }
if (x == 3 && y == 7) { … }
(x == 3 ? alert("Yep") : alert("Nope"))
Proceeds with one of two cases, based on a condition.
If the condition is truthy, it proceeds with the expression after the ?. If falsy, it proceeds with the expression after the :.
Remember the 5 basic data types, and the comparators?
Know that false, 0, "" are equivalently falsy.
var c = (false == 0); // true
var d = (false == ""); // true
var e = (0 == ""); // true
Also, null and undefined are only equivalent to themselves and one-another, but NaN isn't even equivalent to NaN!
var f = (null == false); // false
var g = (null == null); // true
var h = (undefined == undefined); // true
var i = (undefined == null); // true
You can compare both the value AND the type of an object by using the Identical ( ===) or Not Identical ( !==) comparators.
// a single-line comment
/* -------------------------------
A super-fancy multi-line comment
that uses some extra characters to
enhance readability!
-------------------------------- */
Comments are not only useful for helping yourself take notes and keep track of things in your code… they're also often the only way that another coder will be able to understand your code!
var letsStart = confirm("Shall we begin?");
if (letsStart === true) {
userName = prompt("What is your name?");
if (userName !== null && userName !== "") {
nameCount = prompt("Thanks! How many times should I print your name?");
if (nameCount > 0) {
for(c = 0; c < nameCount; c++) {
console.log(userName);
}
} else {
console.log("You must enter a number greater than 0.");
}
} else {
console.log("You must enter a name. Please try again.");
}
} else {
console.log("I can not begin without your name. Please try again.");
}
Functions are a way to take blocks of code and save them for future use.
function myFunction() { … };
var myFunction = function() { … };
Functions can define parameters that allow you to change the output of the function each time you call it, by passing in arguments.
myFunction(argument1, argument2);
var creepyChant = function(userName, c) {
while (c > 0) {
console.log("All work and no play makes " + userName + " a dull girl.");
c--;
}
};
creepyChant("Lauren", 3);
Use return to pass data back out of the function to the point at which it was called:
var getUserName = function() {
var fname = prompt("What is your first name?");
var lname = prompt("What is your last name?");
return fname + " " + lname;
}
console.log(getUserName());
"Lauren Herda"
An object is a collection of data set into named slots.
"Lauren".length;
6
"Lauren".substring(0, 3);
"Lau"
NB: .log() is a method of the console object!
var dinner = new Pizza();
This is done using the new keyword.
dinner.crustType = "Crispy";
var familyPet = {
species: "dog",
breed: "German Shepard",
name: "Abby",
birthday: new Date(2011, 4, 11),
age: function() {
var millisecondsInYear = 31536000000;
return Math.floor((Date.now() - this.birthday) / millisecondsInYear);
}
};
Literals are fixed values—not variables—that you literally provide in your script.
JS provides many built-in advanced data types based on objects (like Date). One of these is Array. Array has a handy constructor shortcut:
var favoriteToppings = ["Pepperoni", "Mushrooms"];
favoriteToppings.push("Sausage");
dinner.toppings = favoriteToppings;
dinner.toppings[0]
"Pepperoni"
Note: the values are addressed starting at 0, not 1.
dinner.dinnertime["start"] = new Date(2013,09,22,17)
Sun Sept 22 2013 17:00:00 GMT-0400 (EDT)
These are associative arrays, and they're similar to objects!
dinner.dinnertime.start
While JS has many built-in DOM tools, jQuery makes it all much simpler.
$("span.highlight");
The $ function allows you to select elements on the page.
$("button").click(<function>);
$("*")
Selects all elements.
$(".classname")
Selects all elements with the given class.
$("div")
Selects all elements with the given tag name.
$("#main")
Selects all elements with the given ID attribute.
$(".classname, #idname, span")
Selects a combined set of elements based on the selectors.
$(document).ready(<function>);
$(document).ready(function() {
console.log("Ready!");
}
By Lauren Herda
The basics of the JavaScript language and how to use it in your Web browser.
Lauren is an Interactive Art Director at @ Mindgrub. Formerly @ Figure 53, Chalk & Chisel, and MICA