Javascript Notes

Variables

You can change them anytime in your program.
var blackjack = 21; (first time use var, but when you reassign the value you don't need to use var again)
undefined
blackjack
21
blackjack = 11;
11 (It resaved as the value 11)

console.log("Hello world"); Hello world

String

String is a quote using "". Using quotes inside of strings.
"Hello, my name is "Kamila". What's yours?"
^ It won't work because the computer can't differentiate between the quotes.
You can invert the quote type > "Hello my name is \"kamila\" whats yours?"
^ This is called an escape character ^

Quotes

HTML entity values (&#)
"Hello, my name is “Kamila”. What's yours?"
^ will correctly make directional quotes. ^

Boolean

Means true and false.

Array

A way of encapsulating bits of datas. You can put strings, numbers, and booleans.
["one", "two", "three"] = array
["one", "two", "three"] [0]
"one" (Javascripts starts at 0 which is the first position in the array)
["one", "two", "three"] .length
3
["one", "two", "three"] [1]
"two"

Relational Operators

Comupter can conceptually understand values and their relationship.
Can check/compute numeric values, but not so good at checking strings (words).
10 < 20
true (This is a boolean)
10 < 10
false
10 <= 10
true

To check equivalency in strings:
"hello" == "world" (single equals is assigning a value)
false
"hello" == "hello"
true
"hello" == "hello" && "hello" == "world"
false (both have to be true to return true)
"hello" == "hello" || "hello" == "world"

if (10 > 5) {
console.log("yes");
}
yes
if (10 > 20) {
console.log("yes");
}
undefined

If Else

if (name == "brendan" || school == "parsons") {
console.log("Hello");
} else {
console.log("Not true...);
}

Shortcuts

command k clears info
shift return creates break

Counter

counter = 1
counter +

For Loop

for (Var multiplier = 1; multiplier <=10; multiplier++) { (when the loop begins, how long it runs and when it ends) console.log(multiplier + 10);
}
11
12
13
... (use this in an array)
var colors = ["red", "yellow", "blue", "green"]
undefined
colors[2]
blue

for (var index = 0; index <= 3; index++) {
console.log(colors[index]);
}
red
yellow
blue
green
**colors.length computes # of elements in the array automatically.**

April 2, 2015 Lab Notes

FUNCTIONS

are used to perform reuseable tasks. Don't use numbers in func. name


function myFunction(arg1, arg2) {
console.log("Hello world");
}
myFunction() ...need 2 parenthesis to call the function.
> Hello World

ADDING 2 Numbers Together

function addNums(num1, num2) {
var sum = num1 + num2;
console.log(sum); ...this is the answer
}
addNums(4,5);
>9

RETURN FUNCTION

instead of console.log

function add(num1, num2) {
var sum = num1 + num2;
return sum;
}
add(20,30)
>50

var mySum = add(20,30); ...you can store return values this way
mySum
>50

OBJECTS

allow us to store complex data in JS variables, with key values

var myArray = ["hello", 2, "world", false]
>undefined
myArray[0]
>"hello"
...objects use {} to define functions and seperated by commas
var myObject = {
name; "Kamila",
age: 20,
currentLocations: "New York, NY"
}
undefined
myObject.name
>Kamila
myObject.age
>20
myObject.name = Sally
>Sally ... to rename objects

JQUERY


always begins with a $. allows us to manipulate the document object model DOM(html source)
$(name of the element we want to define)
$("a"); ...target anchor tags
>all the anchor content
$("h1");
>[] ...there are no h1 on the page
$(".band"); ...use this to find classes
$(".band").html("Hello world");
>allows you to edit html in the Jquery function
$(".band").html("background-color", "red");
>how you change css in Jquery
$(".band").hide();
>hides the band. In the css it says "display: none;"
$(".band").show(); ...to bring it back
$(".band").remove(); ...permanently removes it from the DOM

toggle class switches in between adding and removing it.
.append adds at the end
.prepend add before the content

CLICK STATE

with if statement

var clicked = false;
$(".course-info").click(function() {
// do what I say in here...
if (clicked == false) {
$("body").css("background-color", "red");
clicked = true;
} else { //making it back to white
clicked = false;
$("body").css("background-color", "white");
}
});
> has been attached to the item on the page

Mouse hover

.mousemove
.mouseenter
.mouseup
.mousedown

console.log("smiley face pattern"); for (var multiplier = 1; multiplier < 10; multiplier++){ console.log (multiplier*10); var smiley = "☻"; for (var a = 0; a < 5; a++) { smiley = smiley + "☻☺☻"; console.log(smiley); } }