Guillermo Rodas
Google Developer Expert in Web Technologies, Community Organizer, and Online Teacher.
@garethderioth
Medellin, Nov 9, 2015
Variables
Scope
Objects
Functions
Arrays
var name = 'William';
var age = 23;
var name = 'Wiliam', age = 23, human = true;
var name = 'William',
age = 23,
human = true;
name = 'Marcelo';
human = false;
age = 'Veintitrés';
var salary = 200,
expenses = 100;
var total = (salary - expenses);
var myFriend;
var my_friend;
var MyFriend;
// Invalid names
var 5riend;
var my-friend;
// Valid names
var π = Math.PI;
var ಠ_ಠ = eval;
function calcMoney(salary, expenses) {
return salary - expenses;
}
calcMoney(500, 100);
var sum = function(a, b) {
return a + b;
};
sum(2, 1);
sum = function(a, b, c, d) {
return a + b + c + d;
}
sum(2, 1);
sum(2, 1, 0, 0);
function helloWorld() {
var message = 'Hello world';
return message;
}
var things = function() {
var foo = 'bar';
};
things();
foo;
var greeting = 'hi';
var greets = function() {
var greeting = 'Hello';
};
greeting;
greets();
greeting;
var greeting = 'hi';
var greets = function() {
greeting = 'Hello';
};
greeting;
greets();
greeting;
var person = {
firstName: 'William',
lastName: 'Rhodes',
age: 23,
teacher: true,
religion: undefined,
car: null
};
person.name;
person.car = 'KIA';
person.children = {
childOne: {
name: 'Matthew',
isACat: true
},
childTwo: {
name: 'Leeloo'
}
};
person.children.childOne.name;
person.children.childTwo.isACat;
var person = {
walk: function() {
console.log('Walking');
}
};
var anotherPerson = person;
anotherPerson.walk;
anotherPerson.walk();
anotherPerson.getAge = function() {
return 32;
};
var anotherPersonAge = anotherPerson.getAge();
var newAge = anotherPersonAge
newAge = newAge + 20 ;
newAge;
anotherPersonAge;
anotherPerson.age = 66;
person.age;
var list = [ 'eggs', 'thing', 'carrots', 'some' ];
var numbers = [1, 3, 5];
list[0];
numbers[2];
numbers[5] = 'infinite';
numbers[4];
list.length;
list.join(', ');
list.push('strawberries')
var ALotOfPeople = [
{ name: 'Marcelo' },
{ name: 'Nancy' }
];
var person = ALotOfPeople[1];
ALotOfPeople[1];
ALotOfPeople[1].name;
person['name'];
person['nombre'] = 'Alan Britto';
ALotOfPeople[1].name;
Juan and Pedro won a lottery game and each of them took the 50% of the prize.
David, Pablo and Luisa are sons of Juan. Juan will give the fifth part to a David, three octaves parts to Pablo and two sixths parts to Luisa. He left with $3025.
Pedro will give two sevenths parts to each of his two sons. He left with three senvenths parts.
How many had Juan and Pedro?
The mean of "this"
IIterations
Error handling
Conditionals
Operators
var Utils = {
door: 0,
openDoor: function() {
var that = this;
this.door = 1;
function openAgain() {
this.door += 1;
that.door += 1;
}
openAgain();
}
};
var App = {
init: function() {
this.cacheElements();
this.bindEvents();
Utils.openDoor();
},
cacheElements: function() {
// TODO
},
bindEvents: function() {
// TODO
}
};
App.init();
try {
throw "Ooops!!! something happened";
} catch (err) {
console.log(err);
}
try {
throw new Error("Shot me down!");
} catch(err) {
console.log(err.message);
console.log(err.stack);
}
var arr = [];
arr[1] = 'ohaiou';
arr[2] = 'morning';
for (var prop in arr) {
console.log(prop, arr[prop]);
}
var person = {
name : 'Tom',
age: 23
};
person.name;
person['name'];
for ( var prop in person ) {
console.log(prop);
}
var foo = 'hi';
if ( !parseFloat(foo) ) {
console.log(!parseFloat(foo));
}
foo = '0';
if ( !parseFloat(foo) ) {
console.log(!parseFloat(foo));
}
var baz = [];
if ( baz ) {
console.log(baz);
}
if ( baz.length ) {
console.log(baz.length);
}
false
null
undefined
''
0
NaN
void 0
function giveMeAName(options) {
var name = (options && options.name) || 'Charlie';
console.log(name);
}
giveMeAName({ name: 'March' });
giveMeAName({});
giveMeAName({ name: '' });
giveMeAName(null);
var a = [1, 2, 3, 4];
var b = [1, 2, 3, 4];
var c = { x: 1, y: 2, z: 3 };
var d = { x: 1, y: 2, z: 3 };
var e = "hello";
var f = "he" + "llo";
console.log((a == b));
console.log((a === b));
console.log((c == d));
console.log((c === d));
console.log((e == f));
console.log((e === f));
var a = [1,2,3,4];
var b = [1,2,3,4];
var c = a;
console.log(a === b);
console.log(a === c);
var number = 1,
string = "1",
bool = false;
number;
number + "";
string;
+string;
+string++;
string;
bool;
+bool;
bool + "";
var number = 1,
string = "1",
bool = true;
string === number;
string === number + "";
+string === number;
bool === number;
+bool === number;
bool === string;
bool === !!string;
var array = [ "a", "b", "c" ];
!!~array.indexOf("a");
if ( array.indexOf("a") >= 0 ) {
console.log(array.indexOf("a"));
}
var num = 2.5;
parseInt( num, 10 );
~~num;
num >> 0;
num >>> 0;
var neg = -2.5;
parseInt( neg, 10 );
~~neg;
neg >> 0;
neg >>> 0;
By Guillermo Rodas
A basic introduction to ECMAScript 5 (JavaScript)
Google Developer Expert in Web Technologies, Community Organizer, and Online Teacher.