Created
August 9, 2016 07:38
-
-
Save Papillard/8dc9b6cfd2e6cd36b8f98a8f39108d76 to your computer and use it in GitHub Desktop.
Cheatsheet going through all Javascript programming basics (that you already know in ruby!)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 ------------ Basics ------------ | |
// console.log | |
console.log("hello london!"); | |
// variable / typeof | |
var name = "Seb Saunier"; | |
var lastName = "Paillard"; | |
var age = 17; | |
var weight= 72.6; | |
console.log(typeof(lastName)); | |
console.log(typeof(age)); | |
console.log(typeof(weight)); | |
// 2 ------------ Array / Object ------------ | |
// Array | |
var beatles = ["paul", "john", "ringo", "george"]; | |
console.log(beatles[2]); | |
// Object (like ruby hash) | |
var person = { name: 'bob', age: 42 } // same as { 'name': 'bob', 'age': 42 } | |
console.log(person["name"]); | |
console.log(person.name); | |
// 3 ------------ if / else if / else ------------ | |
if (age >= 21) { | |
console.log("You can be president"); | |
} else if (age > 18) { | |
console.log("You can vote!"); | |
} else { | |
console.log("Can't vote yet"); | |
} | |
// 4 ------------ loop (for / forEach / Object.keys) ------------ | |
// for | |
for (var i = 0; i <= 10; i += 2) { | |
console.log(i); | |
} | |
for (var i = 0; i < beatles.length; i += 1) { | |
console.log(beatles[i]); | |
} | |
// forEach | |
beatles.forEach(function(beatle) { | |
console.log(beatle); | |
}); | |
// Object.keys | |
var instruments = { "john": "guitar", "paul": "bass", "ringo": "drums", "george": "guitar" }; | |
Object.keys(instruments).forEach(function(beatle) { | |
console.log(beatle + " played the " + instruments[beatle]); | |
}); | |
// 5 ------------ define function ------------ | |
function fullName(firstName, lastName) { | |
return firstName + " " + lastName; | |
} | |
var fullName = function(firstName, lastName) { | |
return firstName + " " + lastName; | |
} | |
var result = fullName("seb", "saunier"); | |
console.log(result); | |
// 6 ------------ define class ------------ | |
var Person = function(firstName, lastName) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
Person.prototype.fullName = function() { | |
return this.firstName + " " + this.lastName; | |
} | |
var romain = new Person("Romain", "Paillard"); | |
console.log(romain.fullName()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment