Skip to content

Instantly share code, notes, and snippets.

View dalkegama's full-sized avatar
🤖

Dhanik Alkegama dalkegama

🤖
View GitHub Profile
const someStringNum = "3.5";
+someStringNum + 2; // returns 5.5
+null // returns 0
const someNum = 30;
// Long
someNum.toString(); // returns "30"
10.toString(); // returns "10"
// Shorthand 👍🏽
`${someNum}`; // returns "30"
`10` // returns "10"
const arrayOne = [1,2,3];
// Long way
const newArrayOld = arrayOne.slice(); // returns [1,2,3]
// Shorthand 👍🏽
const newArray = [...arrayOne]; // returns [1,2,3]
let arrayOne = [2, 4, 1];
let arrayTwo = [1, 3, 5];
//Long way
arrayOne.concat(arr2); // returns [ 2, 4, 1, 1, 3, 5 ]
// Shorthand 👍🏽
[...arr1, ...arr2]; // returns [ 2, 4, 1, 1, 3, 5 ]
const someArray = [1,3,2,1,2,3];
[...new Set(someArray)]; // returns [ 1, 3, 2 ];
const someString = "Dhanik";
// Long
someString.charAt(0); //returns "D"
// Shorthand
someString[0]; //returns "D"
@dalkegama
dalkegama / truthy-check-short.js
Last active January 19, 2020 20:12
Medium - JavaScript can do that?
if(someCondition){
console.log("Hello World");
}
// Same if condition in a single line. 👍🏽
if(someCondition) console.log("Hello World");
@dalkegama
dalkegama / truthy-check-long.js
Last active March 26, 2020 13:01
Medium - JavaScript can do that?
if(someCondition !== undefined && someCondition !== null){
// handle whatever
}
const objOne = { foo: "bar", x: 2 };
const objTwo = { foo: "bar", y: 5};
// Long way
const copiedObj = Object.assign({},objOne); // Object { foo: "bar", x: 2 }
const mergedObj = Object.assign(objOne,objTwo); //Object { foo: "bar", x: 2, y:5 }
// Shorthand way 👍🏽
const copiedObj = {...objOne}; // Object { foo: "bar", x: 2 }
@dalkegama
dalkegama / template.literals.js
Last active January 12, 2020 22:16
template.literals.js
const firstNum = 10;
const secNum = 20;
//Shorthand way 👍🏽
console.log(`Hi, this is how we should concatinate
with a multiliine string`);
//Hi, this is how we should concatinate
//with a multiliine string
//Shorthand way 👍🏽(Expression interpolation)