Skip to content

Instantly share code, notes, and snippets.

View gustavoalbuquerquebr's full-sized avatar

Gustavo Albuquerque gustavoalbuquerquebr

View GitHub Profile
// var is hoisted and initialized with undefined
console.log(a); // undefined
var a = 10;
// let and const are hoisted but not initialised (temporal dead zone)
console.log(b); // ReferenceError
let b = 20;
// function declarations: declaration and definition are hoisted
c();
// var is function scope, let and const are block scope
for (var a = 0; a < 5; a++) {}
console.log(a); // 5
for (let b = 0; b < 5; b++) {}
console.log(b); // Reference Error
// global var is added to the window object, let and const aren't
var c = 10;
console.log(window.c); // 10
let d = 20;
@gustavoalbuquerquebr
gustavoalbuquerquebr / by value and by reference.js
Last active June 3, 2020 02:28
#primitives #objects #value #reference
// primitive values are copied by value
let a = 10;
let b = a;
b++;
console.log(a, b); // 10 11
// objects are copied by reference
let c = [12, 21];
let d = c;
d.pop();
@gustavoalbuquerquebr
gustavoalbuquerquebr / falsy values.js
Last active June 3, 2020 02:28
#falsy #false #undefined #null #NaN
// A falsy value is a value that translates to false when evaluated in a Boolean context
// There are 6 falsy values: false, 0, empty string ("" or ''), NaN, undefined and null.
// Everything else is truthy.
// undefined is returned by the JavaScript engine when:
// - a variable has not been assigned,
// - trying acess a variable var before its declaration (because of hoisting var are initialized to undefined),
// - trying to acessing a inexistent property,
// - expected parameter in a function wasn't passed,
// - executing a function without a return value.
console.log(Number("100")); // 100
console.log(Number("100str")); // Nan
console.log(Number("100.5")); // 100.5
console.log(Number("100.5str")); // Nan
console.log(parseInt("100.str")); // 100
console.log(parseInt("str100.str")); // NaN
console.log(parseFloat("100.5str")); // 100.5
console.log(parseFloat("str100.5str")); // NaN
@gustavoalbuquerquebr
gustavoalbuquerquebr / scope chain.js
Last active June 3, 2020 02:28
#scope #scopeChain
// Scope Chain it's what define which variables are available to a specific execution context.
// It's lexical, defined by its location of its definition within the source code, not by where a function is called.
// Hence, if a variable is not available in the local environment the function always go searching
// one level up from where is written until it reaches global level.
// function c is lexically sitting under the global environment,
// so if don't find b in its own scope it will look in global environment, never in function a
function a() {
let b = 10;
c();
@gustavoalbuquerquebr
gustavoalbuquerquebr / variables with dynamic names (variable variables).js
Last active June 3, 2020 02:28
#dynamicName #variableVariables #computedProperties #computed
// dot notation and brackets with quotes is used to acess properties
var a = "str";
window.a = "str replaced!";
console.log(a); // "str replaced!"
var a2 = "str";
window["a2"] = "str replaced!";
console.log(a2); // "str replaced!"
// computed property: generating a property name using any valid JavaScript expression inside square brackets
var b = "str";
@gustavoalbuquerquebr
gustavoalbuquerquebr / strings methods.js
Last active June 3, 2020 02:28
#strings #stringMethods
let a = "Hello world";
let b = "!";
let c = "!";
// Those methods does not change the original string, they only return a new one
console.log(a.length); // 11
console.log(a[0]); // H
console.log(a.charAt(0)); // H
console.log(a.toUpperCase()); // HELLO WORLD
console.log(a.toLowerCase()); // hello world
@gustavoalbuquerquebr
gustavoalbuquerquebr / date.js
Last active June 3, 2020 02:28
#Date #date #time
// Unix time is a system for describing a point in time,
// defined as the number of seconds that have elapsed
// since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970
// Date objects are static. The computer time is ticking, but date objects are not.
// current date and time according to system settings for timezone offset;
// returns a object with methods to get and set date
let a = new Date();
console.log(typeof a); // object
@gustavoalbuquerquebr
gustavoalbuquerquebr / random numbers.js
Last active June 3, 2020 02:28
#Math #numbers #random
let a = 4.7;
let b = 4.3;
let c = [48, 12, 60, 21];
console.log(Math.round(a)); // 5
console.log(Math.round(b)); // 4
console.log(Math.ceil(a)); // 5
console.log(Math.floor(a)); // 4
// returns string representing the given number using fixed-point notation