get started
Declaring variables
// Longhand
let x;
let y = 20;
// Shorthand
let x, y = 20;
Assigning values to multiple variables
// Longhand
let a, b, c;
a = 5;
b = 8;
c = 12;
// Shorthand
let [a, b, c] = [5, 8, 12];
The Ternary operator
// Longhand
let result;
let marks = GetNumber();
if(marks >= 56){
result = 'Pass';
}else{
result = 'Fail';
}
// Shorthand
let result = GetNumber() >= 56 ? 'Pass' : 'Fail';
AND(&&) Short circuit evaluation
// Longhand
if (isLoggedin) {
goToHomepage();
}
// Shorthand
isLoggedin && goToHomepage();
Swap two variables
let x = 'Hello', y = 55;
// Longhand
const temp = x;
x = y;
y = temp;
// Shorthand
[x, y] = [y, x];
Assigning default value
// Longhand
let imagePath;
let path = getImagePath();
if(path !== null && path !== undefined) {
imagePath = path;
} else {
imagePath = 'default.jpg';
}
// Shorthand
let imagePath = getImagePath() || 'default.jpg';
Arrow Function
// Longhand
function add(num1, num2) {
return num1 + num2;
}
// Shorthand
const add = (num1, num2) => num1 + num2;
Reference: JavaScript Arrow function
Object Property Assignment
let firstname = 'John';
let lastname = 'Doe';
// Longhand
let obj = {firstname: firstname,
lastname: lastname};
// Shorthand
let obj = {firstname, lastname};
Multi-line String
// Longhand
console.log('JavaScript, often abbreviated\n' +
'as JS, is a programming language that \n' +
'conforms to the ECMAScript specification. \n' +
'JavaScript is high-level, often \n' +
'just-in-time compiled, and multi-paradigm.' );
// Shorthand
console.log(`JavaScript, often abbreviated
as JS, is a programming language that
conforms to the ECMAScript specification.
JavaScript is high-level, often
just-in-time compiled, and multi-paradigm.`);
String into a Number
// Longhand
let total = parseInt('453');
let average = parseFloat('42.6');
// Shorthand
let total = +'453';
let average = +'42.6';
Double NOT bitwise operator (~~)
// Longhand
const floor = Math.floor(6.8); // 6
// Shorthand
const floor = ~~6.8; // 6
Reference: Math.floor()
Repeat a string for multiple times
// Longhand
let str = '';
for(let i = 0; i < 5; i ++) {
str += 'Hello ';
}
console.log(str); // Hello Hello Hello Hello Hello
// Shorthand
'Hello '.repeat(5);
Find max and min number in array
const arr = [2, 8, 15, 4];
// Longhand
let min = 100, max = 0
for(let i = 0; i < arr.length ; i ++) {
if(i >= max){
max = i;
}
if(i <= min){
min = i;
}
}
// Shorthand
Math.max(...arr); // 15
Math.min(...arr); // 2
For loop
let arr = [10, 20, 30, 40];
// Longhand
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
// Shorthand
for (const val of arr) {
console.log(val);
}
// Shorthand
for (const index in arr) {
console.log(`index: ${index},
value: ${arr[index]}`);
}
Template Literals
// Longhand
console.log('Call from ' + number + ' at ' + time);
// Shorthand
console.log(`Call from ${number} at ${time}`);
Get character from string
let str = 'Hello world';
// Longhand
str.charAt(2); // e
// Shorthand
str[2]; // e
Exponent Power
// Longhand
const power = Math.pow(4, 3); // 64
// Shorthand
const power = 4**3; // 64
Merging of arrays
let arr = [20, 30];
// Longhand
let arr2 = arr.concat([60, 80]);
// [20, 30, 60, 80]
// Shorthand
let arr2 = [...arr, 60, 80];
// [20, 30, 60, 80]
Loop in object
let obj = {x: 20, y: 50};
// Longhand
const objectArray = Object.keys(obj);
for (let i = 0; i < objectArray.length; i++) {
console.log(obj[objectArray[i]]);
}
// Shorthand
for (const key in obj) {
console.log(obj[key]);
}
Multiple condition checking
// Longhand
if (value === 1 ||
value === 'one' ||
value === 2 ||
value === 'two') {
// Execute some code
}
// Shorthand
if ([1, 'one', 2, 'two'].indexOf(value) >= 0) {
// Execute some code
}
// Shorthand
if ([1, 'one', 2, 'two'].includes(value)) {
// Execute some code
}
Deep cloning of multi-level object
let obj = {x: 20, y: {z: 30}};
// Longhand
const makeDeepClone = (obj) => {
let newObject = {};
Object.keys(obj).map(key => {
if(typeof obj[key] === 'object'){
newObject[key] = makeDeepClone(obj[key]);
} else {
newObject[key] = obj[key];
}
});
return newObject;
}
const cloneObj = makeDeepClone(obj);
// Shorthand
const cloneObj = JSON.parse(JSON.stringify(obj));
//Shorthand for single level object
let obj = {x: 20, y: 'hello'};
const cloneObj = {...obj};