Ternary Operator
//Longhead
let response;
if (x < 5){
response = "You don't have enough cats!"
} else{
response = "You have too many cats!"
}
//Shorthand
const response = x < 5 ? "You don't have enough cats!" : "You have too many cats!"
Assign Values to Multiple Variables
//Longhand
let x = 143;
let y = 9;
let z = 66;
//Shorthand
let [x, y, z] = [143, 9, 66];
Arrow Function
//Longhand
let square = function(i){
return i*i;
}
//Shorthand
let square = i => i * i;
Template Literals
let length = 10;
let width = 20;
//Longhead
console.log("The area of your square is" + length + "*" + width);
//Shorthand
console.log(`The area of your square is ${length} * ${width}`);
Multi-line String
//Longhand
console.log('In case you were wondering ' + 'There is a franchise of movies ' + 'about tornados ' + 'with sharks ' + 'inside of them.');
//Shorthand
console.log(`In case you were wondering
there is a franchise of movies
about tornados
with sharks
inside of them.`);
String into a Number
let userInput = "28";
//Longhand
let age = parseInt(userInput)
//Shorthand
let age = +userInput;
Repeat a String
//Longhand
let text = '';
for( let i = 0l i < 10; i++){
text +="Javascript is fun!";
}
//Shorthand
console.log('Javascript is fun!'.repeat(10));
Substitute for Math.floor()
let num = 99.99;
//Longhead
console.log(Math.floor(num));
//Shorthand
console.log(~~num);
Min/Max Numbers in an Array
let grades = [99, 95, 92, 84, 76, 65, 52, 8];
//Longhand
Math.max(99, 95, 92, 84, 76, 65, 52, 8)
//Shorthand
Math.max(...grades);
Merging of Arrays
let nineties = [1997, 1998, 1999];
//Longhand
let genz = nineties.concat([2000, 2001, 2002]);
//Shorthand
let genz = [...nineties, 2000, 2001, 2002];