- used to build responsive websites, build apps/games, access info, organize and present data
- must be in <script></script> tags
document.getElementById("myImage").src = "pic.gif";
- innerHTML defines HTML content
document.getElementById("demo").innerHTML= 'ex' - printing for testing purposes, if at the end will delete everything on the HTML page
document.write(5) - printing for debugging purposes
console.log(5) - popup window
window.alert(5)
- var:
var varName = data; - break
- continue: jumps out of a loop, starts at the top
- try...catch
- return: exits a function
- dynamic: same variable can hold different data types
- arrays: [item1, item2],
var a = new Array() - Objects: {firstName:"john", lastName: "Doe"}
- undefined: variable without a value
- empty value: "" is a string
- null: nothing (var person = null)
parseInt(): convert String to integer- 1/0 is Infinity, -1/0 is -Infinity
isFinite(): test for infinity, -infinity, Nan
'hello'.length: 5'hello'.charAt(0): h'hello'.replace('hello', 'goodbye'): goodbye'hello'.toUpperCase(): HELLO
let: declared variable is available from function block it's enclosed inconst: constantsvar: no restrictions
- Comparison:
>,<===: equal to!==: not equal to===: equal value and equal type
- Logic
&&,||,!
- Strings
- Concatenate:
+
- Concatenate:
- Type
typeof something: tells you the type of something (ex. number, string, object)obj.instanceof(): returns true if object is an instance of an object type
if (){
console.log();
}
else{
}- switch: avoid a large amount of if/else statements
switch(prompt("question"){
case "1":
//statements
break;
case "2":
//statements
break;
default:
//statements
break;
}- for loops
for (var i = 0; i < 10; i++){
statements;
}- while loops
while (bool){
statements;
}- do loops: runs once before it checks the while condition
do{
statements;
} while (bool); - invoked when somehting calls the function (through an event, self invoked, or called from Javascript code)
- parameters are guidelines (can call function without passing expected parameters)
arguments: variable that is an array like object which holds all values of the function
function name(parameter1, parameter2, parameter3){
code to be executed
return;
}
Anonymous Functions:
- allows you to put full function anywhere you'd normally put an expression
var name = function(){
// code to be executed
}
### Callback Functions
* derived from functional programming - use functions as arguments
### Objects
* all objects can be thought of as a dictionary
* Access Properties: `object.PropertyName` or `object[PropertyName]`
* Access Methods: `object.methodName()`
* `this` keyword: refers to whatever object the method was called on (placeholder), use when creating new objects
### Creating Objects/Classes
* uses functions for classes
Define an Object:
```javascript
function Person(first, last) {
this.first = first;
this.last = last;
}
Create new Object:
var name = new Object();- execute code when an event ahppens
- extend the prototype: allow all members of a class to use a certain method
- prototype chain: if Javascript can't find a method in the current class, it moves up the chain until it gets to the Object class
function Person(first, last) {
this.first = first;
this.last = last;
}
Person.prototype.fullName = function(){
return this.first + ' ' + this.last;
}
Person.prototype.fullNameReversed = function() {
return this.last + ', ' + this.first;
}- X is-a Y relationship: Y's protype needs to be Y (Ex. a penguin is an animal. Therefore, penguin's prototype is animal)
- penguin inherits properties from animal:
penguin.prototype = new Animal();- combination of function and scope object
function makeAdder(a){
return function(b){
return a+b;
};
}
var x = makeAdder(5)
x(6) // returns 11
- document: any web page loaded into browser, serves as entry point into web page's content
- element: element or node of type element (
document.createElement()) - nodeList: array of elements (
document.getElementsByTagName()) - attribute: object reference that exposes small interface for attributes (
createAttribute) - namedNodeMap: array with items accessed by name/index
- document: root of document itself
- window: represents something like the browser, inherits from generic Node interface
document.getElementById(id)document.getElementsByTagName(name)document.createElement(name)Node.appendChild(node): adds node to end of list of children of specified parent nodeelement.innerHTML: property sets or gets HTML syntax describing element's descendantselement.style.left: get and set inline style of elementelement.getAttribute(id): get the value of a certain element with specified idelement.addEventListenerwindow.onloadwindow.dumpwindow.scrollTo(x-coord, y-coord): scrolls to particular set of coordinates