Skip to content

Instantly share code, notes, and snippets.

View iamsaief's full-sized avatar
🚀
Gotta debug before I sleep ⏳

Saief Al Emon iamsaief

🚀
Gotta debug before I sleep ⏳
View GitHub Profile
/* Truthy/Falsy value */
let age = 0;
if (age) {
console.log(true);
} else {
console.log(false);
}
// Output : false
let name; // undefined, falsy
/* unassigned variable */
let name;
console.log(name);
// Output: undefined
/* No return/ nothing after return */
function add(num1, num2) {
result = num1 + num2;
return;
}
/* 👎 double equal == */
const first = 1;
const second = "1";
if (first == second) {
console.log(true, `${first} == ${second}`);
} else {
console.log(false, `${first} != ${second}`);
}
// Output : true 1 == 1
/* global/block scope */
let result = 10;
function sum(x, y) {
let result = x + y;
console.log(result); // 35
if (result > 10) {
var msg = "more than 10";
console.log(msg);
}
console.log(msg); // outside scope, because of hoisting
/* Map */
const numbers = [2, 3, 4, 5, 6, 8];
const squaredNum = numbers.map((num) => num * num);
console.log(squaredNum);
// Output : [ 4, 9, 16, 25, 36, 64 ]
/* Filter */
const shapes = ["🟨", "🟩", "🔵", "🔴", "🟨", "🟩", "🔵", "🔴"];
const circle = shapes.filter((item) => item === "🔴");
const square = shapes.filter((item) => item === "🟩");
/* slice(), splice(), join() */
const sports = ["⚽️", "🏀", "🏈", "🏓", "🏸", "🏏", "🏐", "🎱"];
const part = sports.slice(3, 6);
console.log(part);
// Output: ["🏓", "🏸", "🏏"]
const removed = sports.splice(2, 6, "🎭", "🤹");
console.log(removed);
console.log(sports);
/*
/* let, const */
let gf = "Jennifer Lawrence";
console.log(gf);
gf = "Elizabeth Olsen";
console.log(gf);
/**
* Output :
Jennifer Lawrence
Elizabeth Olsen
/* 3 dots: spread */
const red = ["🧡"];
const green = ["💚"];
const blue = ["💙"];
const rgbHeart = [...red, ...green, ...blue];
console.log(rgbHeart);
// Output : ["🧡", "💚", "💙"]
/* destructure: array */
const hearts = ["🧡", "💚", "💙", "💛", "💜", "🖤", "🤍"];
/* Default Parameter */
function addFifty(num1, num2 = 50) {
return num1 + num2;
}
const result = addFifty(10, 20);
const result2 = addFifty(20);
console.log(result, result2);
// Output : 30 70
/* Template string */
/* Arrow functions - implicit return*/
const doubleIt = (num) => num * 2;
console.log(doubleIt(4));
// Output: 8
const add = (a, b) => a + b;
console.log(add(40, 50));
// Output: 90
/* explicit return */