Skip to content

Instantly share code, notes, and snippets.

View lenafaure's full-sized avatar

Lena Faure lenafaure

View GitHub Profile
movieStar(); // Prints “TypeError : movieStar is not a function”
var movieStar = function () {  // This is a function expression and therefore not hoisted
  var name = “Philip Seymour Hoffman”;
  console.log(name); 
}
var movieStar = function () {
  var name = “Philip Seymour Hoffman”;
  console.log(name); 
}
movieStar(); // Prints “Philipp Seymour Hoffman”
var businessStar;
function businessStar(){
  console.log(“Gary Vaynerchuck”);
}
console.log(businessStar); // Prints “function businessStar(){ console.log(“Gary Vaynerchuck”);}”
var businessStar = “Gary Vaynerchuck”;
function businessStar(){
  console.log(“Gary Vaynerchuck”);
}
console.log(businessStar); // Prints “Gary Vaynerchuck”
@lenafaure
lenafaure / local_global_scope.js
Last active July 5, 2017 13:21
Local and global variable scope
var greeting = "Hi, I am a global variable"; // global scope variable
function sayHello() {
var greeting = "Hi, I am a local variable"; // local scope variable
console.log(greeting);
}
sayHello(); // Prints "Hi, I am a local variable"
console.log(greeting); // Prints "Hi, I am a global variable"
@lenafaure
lenafaure / access_global_scope.js
Created April 30, 2017 08:32
Access global scope from inside a function
var greeting = "Hi there!"; // global scope
function sayHello() {
console.log(greeting);
}
sayHello(); // Prints "Hi there !"
@lenafaure
lenafaure / var_keyword.js
Created April 30, 2017 08:41
The var keyword and the scope
var myVariable = "Hi, I am a global variable";
function sayHello() {
myVariable = "Hi, I am locally produced!";
console.log(myVariable);
}
console.log(myVariable); // Prints "Hi, I am a global variable"
sayHello(); // Prints "Hi, I am locally produced!"
console.log(myVariable); // Prints "Hi, I am locally produced!"
@lenafaure
lenafaure / local_prevail.js
Created April 30, 2017 08:44
Local variable prevails
var myJob = "Developer";
function showJob() {
var myJob = "Designer";
console.log(myJob);
}
showJob(); // Prints "Designer"
@lenafaure
lenafaure / place_of_function.js
Created April 30, 2017 08:46
Place of function in the code
var myVariable = "global";
printVariable(); // Prints "local"
function printVariable() {
var myVariable = "local";
console.log(myVariable);
}
printVariable(); // Prints "local"
var customer = {
firstName: "John",
lastName: "Doe",
greetCustomer: function(){
// We use "this" instead of repeating "customer"
console.log("Hello again " + this.firstName + " " + this.lastName + "!");
}
}
customer.greetCustomer(); // Prints "Hello again John Doe!"