Skip to content

Instantly share code, notes, and snippets.

let value = 0;
if (true) {
 // new scope, TDZ of 'value' begins
// When trying to access the variable, we take ReferenceError,
// because right now it's a variable
 // not initialized
 console.log (value); // ReferenceError
let value; // TDZ ends and 'value' is set to 'undefined'
 console.log (value); // undefined
value = 1;
const arrayVar = [];
for (var i = 0; i <5; i ++) {
 arrayVar.push (function () {
 console.log (i);
 });
}
i = 10; // assigning a new value
arrayVar.forEach (function) {
 occupation(); // 10 10 10 10 10
});
arrayVar.forEach (function) {
 occupation(); // 5 5 5 5 5
});
arrayLet.forEach (function) {
 occupation(); // 1 2 3 4 5
});
const arrayLet = [];
for (let i = 1; i <5; i ++) {
 arrayLet.push (function () {
 console.log (i);
 });
}
const arrayVar = [];
for (var i = 1; i <5; i ++) {
 arrayVar.push (function () {
 console.log (i);
 });
}
// examples
var object = {}; // object
var number = 1; // number
var name = "Keys"; // string
var list = [1,2,3]; // list
var song1 = {
 title: 'Love has no rollback',
author: 'SQL'
}
var songs = new Set ([song1]);
console.log (songs);
song1 = null;
var song1 = {
 title: 'Love has no rollback',
 author: 'SQL'
}
var songs = new WeakSet ([song1]);
console.log (songs);
// WeakSet {Object {title: "Love has no rollback", author: "SQL"}}
var songs = new Set ([
 'song1', 'song1', 'song1'
]);
var songsAmount = songs.size;
console.log ("There are" + songsAmount + "songs in the list");
// There are 3 songs in the list
var songs = new Set(['song1']);
if (songs.has ('song1')) {
 console.log ('already in the list!');
}
// is already in the list!