Skip to content

Instantly share code, notes, and snippets.

View maciejtrzcinski's full-sized avatar
👨‍🚀

Maciej Trzciński maciejtrzcinski

👨‍🚀
View GitHub Profile
@maciejtrzcinski
maciejtrzcinski / while.js
Last active October 29, 2018 11:03
Looping over arrays - while
let index = 0;
const array = [1,2,3,4,5,6];
while (index < array.length) {
console.log(array[index]);
index++;
}
const array = [1,2,3,4,5,6];
array.forEach(function(item, index, array) {
console.log(`index ${index}, value ${item}, array ${array}`);
});
var objectArray = {a: 1, b: 2, c: 3};
for (const item in objectArray) {
console.log(item)
}
['Poland', 'Germany', 'France', 'Russia', 'Japan'].sort();
// ["France", "Germany", "Japan", "Poland", "Russia"]
['Hängt', 'Haut', 'Hüllen', 'Hubert'].sort();
// ["Haut", "Hubert", "Hängt", "Hüllen"] Bad
['Hängt', 'Haut', 'Hüllen', 'Hubert'].sort(function (a, b) {
return a.localeCompare(b);
});
//["Hängt", "Haut", "Hubert", "Hüllen"] Good
['Hängt', 'Haut', 'Hüllen', 'Hubert'].sort(Intl.Collator().compare);
//["Hängt", "Haut", "Hubert", "Hüllen"] Good
if (!Object.is) {
Object.is = function(x, y) {
// SameValue algorithm
if (x === y) { // Steps 1-5, 7-10
// Steps 6.b-6.e: +0 != -0
return x !== 0 || 1 / x === 1 / y;
} else {
// Step 6.a: NaN == NaN
return x !== x && y !== y;
}
' ' == 0 //true
null == undefined //true
[1] == true //true
NaN === NaN //false