visit
Update 1: Due to a lot of polarizing comments (like loved or hated the article) I just want to be clear that shorthand are useful sometimes and sometimes not, depends on which context, don’t take shorthand code as the best of any situation!
I’ve made this post as a vital source of reference for learning shorthand JavaScript coding techniques that I have picked up over the years. To help you understand what it going on I have included the longhand versions to give some coding perspective on the shorts.
Longhand
if (variable1 !== null || variable1 !== undefined || variable1 !== '') { let variable2 = variable1; }
Shorthand
let variable2 = variable1 || '';
Don’t believe me? Test it yourself (paste into Chrome Dev Tools and click run):
//null value examplelet variable1 = null;let variable2 = variable1 || '';console.log(variable2);//output: '' (an empty string)//undefined value examplelet variable1 = undefined;let variable2 = variable1 || '';console.log(variable2);//output: '' (an empty string)//normal value examplelet variable1 = 'hi there';let variable2 = variable1 || '';console.log(variable2);//output: 'hi there'
Longhand
let a = new Array(); a[0] = "myString1"; a[1] = "myString2"; a[2] = "myString3";
Shorthand
let a = ["myString1", "myString2", "myString3"];
Longhand:
let big;if (x > 10) { big = true;}else { big = false;}
Shorthand:
let big = x > 10 ? true : false;
If you rely on some of the weak typing characteristics of JavaScript, this can also achieve more concise code. For example, you could reduce the preceding code fragment to this:
let big = (x > 10);
//further nested exampleslet x = 3,big = (x > 10) ? "greater 10" : (x < 5) ? "less 5" : "between 5 and 10";console.log(big); //"less 5"
let x = 20,big = {true: x>10, false : x< =10};console.log(big); //"Object {true=true, false=false}"
Longhand:
let x;let y;let z = 3;
Shorthand:
let x, y, z=3;
Longhand:
x=x+1;minusCount = minusCount - 1;y=y*10;
Shorthand:
x++;minusCount --;y*=10;
Other shorthand operators, given that x=10 and y=5, the table below explains the assignment operators:
x += y //result x=15x -= y //result x=5x *= y //result x=50x /= y //result x=2x %= y //result x=0
Longhand:
var re = new RegExp("\d+(.)+\d+","igm"),result = re.exec("padding 01234 text text 56789 padding");console.log(result); //"01234 text text 56789"
Shorthand:
var result = /d+(.)+d+/igm.exec("padding 01234 text text 56789 padding");console.log(result); //"01234 text text 56789"
if (likeJavaScript === true)
Shorthand:
if (likeJavaScript)
Here is another example. If “c” is NOT equal to true, then do something.
Longhand:
let c;if ( c!= true ) {// do something...}
Shorthand:
let c;if ( !c ) {// do something...}
Longhand:
function myFunction( myString, myNumber, myObject, myArray, myBoolean ) { // do something...}myFunction( "String", 1, [], {}, true );
Shorthand (looks long but only because I have console.log’s in there!):
function myFunction() { console.log( arguments.length ); // Returns 5 for ( i = 0; i < arguments.length; i++ ) { console.log( typeof arguments[i] ); // Returns string, number, object, object, boolean }}myFunction( "String", 1, [], {}, true );
Longhand:
"myString".charAt(0);
Shorthand:
"myString"[0]; // Returns 'm'
Longhand:
function x() {console.log('x')};function y() {console.log('y')};let z = 3;if (z == 3) { x();} else { y();}
Shorthand:
function x() {console.log('x')};function y() {console.log('y')};let z = 3;(z==3?x:y)(); // Short version!
Longhand:
for (let i = 0; i < 10000; i++) {
Shorthand:
for (let i = 0; i < 1e4; i++) {