Skip to content

Instantly share code, notes, and snippets.

View lenafaure's full-sized avatar

Lena Faure lenafaure

View GitHub Profile
var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day [,hour,minute,second,millisecond ]);
for (var i = 0; i < 10; i++) {
(function (j) {
setTimeout(function() {
console.log(j);
}, j*1000);
})(i);
}
for (var i = 0; i < 10; i++) {
function timer(j) {
setTimeout(function() {
console.log(j);
}, j*1000);
};
timer(i);
for (var i = 0; i < 10; i++) {
console.log(i);
setTimeout(function() {
console.log(i);
}, i*1000);
}
function bakeCake(ingredient){
// Code that adds the ingredient to the cake batter
console.log(ingredient+ ' cake : add ' +ingredient+ ' to the batter');
function ovenTemperature(temperature, time){
// Code that sets the right temperature for baking the cake
console.log('Set the oven temperature to ' +temperature+' and ready to bake the '+ingredient+' cake for ' +time+ ' minutes');
}
return ovenTemperature;
}
console.log(myFunction());
// Prints
function add(number2) {
return number1 + number2;
}
function myFunction(number1) {
function add(number2) {
/* The number1 variable is accessible from this function,
because number1 has been defined outside of the add function */
return number1 + number2;
}
for (var i = 0; i < 10; i++) {
setTimeout(function() {
console.log(i);
}, i*1000);
}
var customer = {
firstName: "John",
lastName: "Doe",
greetCustomer: function(){
console.log("Hello again " + this.firstName + " " + this.lastName + "!");
function nestedFunction(){
console.log(this);
}
nestedFunction();
var cat = "Alphonse";
// The variable cat is actually attached to the global window object :
console.log(window.cat === cat); // Prints true
// "this" in the global context has the value of the global window object
console.log(this); // Prints the window object