Skip to content

Instantly share code, notes, and snippets.

View alenvlahovljak's full-sized avatar
🎯
Focusing

Alen Vlahovljak alenvlahovljak

🎯
Focusing
View GitHub Profile
@alenvlahovljak
alenvlahovljak / st_example-1.ts
Last active September 6, 2020 09:09
How to adopt a static typing?
//* Type Inference *//
// The system can guess that array will contain only numbers
// based on the array's initialization.
const arr1 = [14, 21, 28];
arr1.push(15);
arr1.push("Hello World!");
@alenvlahovljak
alenvlahovljak / hcc_example-6.js
Created September 3, 2020 12:10
How to handle comparison corner cases?
const arr1 = [1, 2, 3];
const arr2 = [];
if (arr1) {
console.log("You should see this message!");
}
if (arr1.length) {
console.log("Array is not empty!");
@alenvlahovljak
alenvlahovljak / hcc_example-5.js
Created September 3, 2020 11:50
How to handle comparison corner cases?
var students = [];
//** if(students) **//
// 1. students
// 2. Boolean(students)
if (true) console.log("You can see this message!");
//** if(students == true) **//
// 1. "" == true
@alenvlahovljak
alenvlahovljak / hcc_example-4.js
Created September 3, 2020 11:38
How to handle comparison corner cases?
var students = [];
if (students) {
console.log("You can see this message!");
}
if (students == true) {
console.log("You can't see this message!");
}
@alenvlahovljak
alenvlahovljak / hcc_example-3.js
Last active September 3, 2020 11:26
How to handle comparison corner cases?
var arr1 = [];
var arr2 = [];
//1. arr1 != arr2
//2. (!(arr1 == arr2))
//3. (!(false))
if (true) console.log("It's true again!");
@alenvlahovljak
alenvlahovljak / hcc_example-2.js
Last active September 3, 2020 11:24
How to handle comparison corner cases?
var arr1 = [];
var arr2 = [];
//1. arr1 == !arr2
//2. [] == false
//3. "" == false
//4. "" == 0
//5. 0 == 0
//6. 0 === 0
@alenvlahovljak
alenvlahovljak / hcc_example-1.js
Last active September 3, 2020 11:24
How to handle comparison corner cases?
var arr1 = [];
var arr2 = [];
if (arr1 == !arr2) {
console.log("Yes, it's true!");
}
if (arr1 != arr2) {
console.log("It's true again!");
@alenvlahovljak
alenvlahovljak / aec_example-8.js
Created August 28, 2020 13:41
How does Abstract Equality Comparison work?
var str1 = "Hello";
var str2 = new String("Hello");
str1 == str2 // true
str1 === str2 // false - coercion was not allowed this time
@alenvlahovljak
alenvlahovljak / aec_example-7.js
Last active August 28, 2020 10:27
How does Abstract Equality Comparison work?
// 1. compare number == array
// 2. 42 == ToPrimitive(array)
// 3. 42 == "42"
// 4. 42 === 42
if (true) console.log("Now you know the magic!");
@alenvlahovljak
alenvlahovljak / aec_example-6.js
Created August 28, 2020 10:18
How does Abstract Equality Comparison work?
var number = 42;
var array = [42];
if (number == array) console.log("It works. Is this a magic?");