Skip to content

Commit add987c

Browse files
committed
js shorthand coding technique 🗽🚀
1 parent 1785462 commit add987c

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Longhand:
2+
3+
Math.floor(5.9) === 5; //true
4+
5+
// Shorthand:
6+
7+
~~5.9 === 5; //true
8+
9+
console.log(~~5.9 === 5);

js-coding-technique/exponent-power.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Longhand:
2+
3+
Math.pow(2, 4); // 16
4+
Math.pow(2, 2); // 4
5+
Math.pow(4, 4); // 256
6+
7+
// Shorthand:
8+
9+
2 ** 4; // 16
10+
2 ** 2; // 4
11+
4 ** 4; // 256
12+
13+
console.log(2 ** 4);
14+
console.log(2 ** 2);
15+
console.log(4 ** 4);

js-coding-technique/obj-assignment.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// longhand
2+
let fname = { firstName: 'Black' };
3+
let lname = { lastName: 'Panther' };
4+
5+
// shorthand
6+
let full_names = Object.assign(fname, lname);
7+
8+
// another
9+
let full_names = { ...fname, ...lname };
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Longhand:
2+
3+
const num1 = parseInt('600');
4+
const num2 = parseFloat('600.06');
5+
6+
// Shorthand:
7+
8+
const num6 = +'600'; // converts to int data type
9+
const num2 = +'600.06'; // converts to float data type

0 commit comments

Comments
 (0)