Learning Javascript

The language of the web - easily the most important language out there right now.

Following Hack Reactor Prep:
https://docs.google.com/document/d/1sb6N2_I0FDUO8b6BQmMlHZ-J0ei_oL-TvHuN7tVC31c/edit

A. Start with codeacademy JS

Important things learnt:

1. confirm("this pops up a confirmation box")/ prompt/ alert

2. "some word".substring(xy) where x is where you start chopping and y is where you finish chopping the original string.

3. var sayHello = function(name) {
    console.log('Hello ' + name);
}
4. for loops
for (var counter = 1; counter < 6; counter++) {
console.log(counter);
}

5. Array

var arrayName = [data, data, data];
Any time you see data surrounded by [ ], it is an array.
a. store lists of data
b. can store different data types at the same time
c. are ordered so the position of each piece of data is fixed
Arrays have 0-based indexing, so we start counting the positions from 0.
6. String

You can treat a string like an array of characters. For instance, you know that
var myArray = ['hello', 'world'];
myArray[0];    // equals 'hello'
But this also works on strings!
var myName = 'Eric';
myName[0];    // equals 'E'
7. The do/ while loop

do {
console.log("I'm gonna stop looping 'cause my condition is " + loopCondition + "!");
} while (loopCondition);

8. is NaN
If you call isNaN on something, it checks to see if that thing is not a number. So:
isNaN('berry'); // => true
isNaN(NaN); // => true
isNaN(undefined); // => true
isNaN(42);  // => false

9. Switch statement syntax

switch (/*Some expression*/) {
    case 'option1':
        // Do something
        break;
    case 'option2':
        // Do something else
        break;
    case 'option3':
        // Do a third thing
        break;
    default:
       // Do yet another thing
}

10. Logical operators

&& || !

11. Objects

Using objects, we can put our information and the functions that use that information in the same place.


var myObject = {
    key: value,
    key: value,
    key: value
};
Using constructor to create object:
var myObj = new Object();

You can add keys to your object after you've created it in two ways:
myObj["name"] = "Charlie";
myObj.name = "Charlie";

12. For/ In loop

for (var key in object) {
  // Access that key's value
  // with object[key]
}

The "key" bit can be any placeholder name you like. It's sort of like when you put a placeholder parameter name in a function that takes arguments.


var search = function(name) {
    for(var key in friends) {
        if(friends[key].firstName === name) {
            console.log(friends[key]);
            return friends[key];
        }
    }
};

13. Method

 A method is just like a function associated with an object.

14. Constructor

function Person(name,age) {
  this.name = name;
  this.age = age;
}

REVIEW UP TILL NOW:
// Our Person constructor
function Person(name, age){
this.name = name
this.age = age
}

// Now we can make an array of people
var family = []

// loop through our new array
family[0] = new Person("alice", 40)
family[1] = new Person("bob", 42)
family[2] = new Person("michelle", 8)
family[3] = new Person("timmy", 6)

for (var member in family){
    console.log(family[member].name)
}

15. Adding a method to a constructor
function someObject() {

  this.someMethod = function() {
  };

}
Suppose we said var someObj = newsomeObject();. When we call someObj.someMethod(), the code between the curly brackets } above will run.


Comments