JavaScript smart Hacks
-Nishchit D.

//Expert

function Recharge(amount) {
this.payment = amount;

this.hasBalance = !!amount; // True

}


var doRecharge = new Recharge(100);
//Else

function Recharge(amount) {
this.payment = amount;

if(amount) {

this.hasBalance = true; 

}

}

var doRecharge = new Recharge(100);
Boolean with !!
//Expert

function setAge(age){

return +age;

}


setAge(25) // 25

setAge('ABC') // NaN

//Else

function setAge(age){


return age * 1;

}
String to Number
//Expert

function Car(model, price){



this.price = 1000;

this.model = model || 'Bently';

}
//Else

function Car(model, price){



this.price = 1000;

this.model = model? model: 'Bently';



}
Default value
//Expert



var length = array.length;

for (var i = 0; i < length; i++) {

console.log(array[i]);

}
OR



for (var i = 0,length = array.length; i < length; i++) {

console.log(array[i]);

}
//Else

for (var i = 0; i < array.length; i++){

console.log(array[i]);

}



// This will consume more memory 

// when array is too large
Loop performance
//Expert

if('street' in person.address || {}){


console.log(person.address.street)
// A/2 Park Avenue
}
//Else

if(person 

&& person.address 

&& person.address.street){



console.log(person.address.street)
// A/2 Park Avenue
}
Check object property exists
//Expert

success && goForward();
//Else

if(success){

goForward()

}
Short-circuits condition
Array Hacks
// Expert

var array = [1, 2, 3, 4, 5, 6];



array.slice(-1) // [ 6 ]

array.slice(-2) // [ 5, 6]
//Else

var array = [1, 2, 3, 4, 5, 6];



array.slice(5) // [ 6 ]

array.slice(4) // [ 6 ]
Get Tailing Array


//Expert

// Remove last two

var numbers = [1, 2, 3, 4, 5, 6];

numbers.length = 4; // numbers = [ 1, 2, 3,
4]
//Else


// Remove last two

var numbers = [1, 2, 3, 4, 5, 6];


numbers.splice(4, 2) // remove [ 5, 6 ]
Array Truncation
// Expert

var A1 = [1, 2, 3];

var A2 = [4, 5, 6];



A1.push.apply(A1, A2)
// Else

var A1 = [1, 2, 3];

var A2 = [4, 5, 6];


console.log(array1.concat(array2));
// [1,2,3,4,5,6];

// This will consume more memory
// to create new array
// in case with large array merge.
Array Merge
// To get pure array from Array like Object

var pureArray = [].slice.call(object)

OR

var pureArray = Array.from(object)
NOTE:
1. NodeList from DOM is array of DOM but it
don’t have all array protoypes.
2. Arguments from functions are array like
object.

var object = Object.create(Array.prototype);



object.push("foo");

object.push("bar"); // [ 'foo', 'bar' ]



object.me = "ND"



console.log(object)



/**


object

Array[2]

0: "foo"

1: "bar"

length: 2

me: "ND"

__proto__: Array[0]

**/
Array like Object
Miscellaneous
[10] === 10 // is false

[10] == 10 // is true

'10' == 10 // is true

'10' === 10 // is false

[] == 0 // is true

[] === 0 // is false

'' == false // is true but true == "a" is false

'' === false // is false
== vs ===
var arr = ["a", "b", "c"];
typeof arr; // return "object"

arr instanceof Array // true

arr.constructor(); //[]

typeof, instanceof & constructor
(function(a,b){

var result = a+b;

return result;


})(10,20)
OR
(function(ng, $){

ng.module(‘app’, []);


})(angular, jQuery)
(function(){


// some private code
// that will be executed automatically


})();
pattern

step 1. ()()

step 2. (FN)()

step 3. (FN)(var1, var2, ...)
IIFE- Immediately Invoked Function Expression

OR 

Self-Invoked Anonymous Function.
function callMe(){


var argArray = Array.prototype.slice.call(arguments);


conosle.log(argArray) // [ 1, 2, 3, 4, 5 ]

}



callMe( 1, 2, 3, 4, 5)

Transform the arguments Object into an Array
//Get the max or the min in an array of numbers


var numbers = [ 200, 30, -434, 546, 237, 11 ];


var maxInNumbers = Math.max.apply(Math, numbers);

var minInNumbers = Math.min.apply(Math, numbers);
Thank you !!
Ohh Sorry...

Last thing to mention.


Js hacks

  • 1.
  • 2.
    //Expert
 function Recharge(amount) { this.payment= amount;
 this.hasBalance = !!amount; // True
 } 
 var doRecharge = new Recharge(100); //Else
 function Recharge(amount) { this.payment = amount;
 if(amount) {
 this.hasBalance = true; 
 }
 }
 var doRecharge = new Recharge(100); Boolean with !!
  • 3.
    //Expert
 function setAge(age){
 return +age;
 } 
 setAge(25)// 25
 setAge('ABC') // NaN
 //Else
 function setAge(age){ 
 return age * 1;
 } String to Number
  • 4.
    //Expert
 function Car(model, price){
 
 this.price= 1000;
 this.model = model || 'Bently';
 } //Else
 function Car(model, price){
 
 this.price = 1000;
 this.model = model? model: 'Bently';
 
 } Default value
  • 5.
    //Expert
 
 var length =array.length;
 for (var i = 0; i < length; i++) {
 console.log(array[i]);
 } OR
 
 for (var i = 0,length = array.length; i < length; i++) {
 console.log(array[i]);
 } //Else
 for (var i = 0; i < array.length; i++){
 console.log(array[i]);
 }
 
 // This will consume more memory 
 // when array is too large Loop performance
  • 6.
    //Expert
 if('street' in person.address|| {}){ 
 console.log(person.address.street) // A/2 Park Avenue } //Else
 if(person 
 && person.address 
 && person.address.street){
 
 console.log(person.address.street) // A/2 Park Avenue } Check object property exists
  • 7.
  • 8.
  • 9.
    // Expert
 var array= [1, 2, 3, 4, 5, 6];
 
 array.slice(-1) // [ 6 ]
 array.slice(-2) // [ 5, 6] //Else
 var array = [1, 2, 3, 4, 5, 6];
 
 array.slice(5) // [ 6 ]
 array.slice(4) // [ 6 ] Get Tailing Array
  • 10.
    
 //Expert
 // Remove lasttwo
 var numbers = [1, 2, 3, 4, 5, 6];
 numbers.length = 4; // numbers = [ 1, 2, 3, 4] //Else 
 // Remove last two
 var numbers = [1, 2, 3, 4, 5, 6]; 
 numbers.splice(4, 2) // remove [ 5, 6 ] Array Truncation
  • 11.
    // Expert
 var A1= [1, 2, 3];
 var A2 = [4, 5, 6];
 
 A1.push.apply(A1, A2) // Else
 var A1 = [1, 2, 3];
 var A2 = [4, 5, 6]; 
 console.log(array1.concat(array2)); // [1,2,3,4,5,6];
 // This will consume more memory // to create new array // in case with large array merge. Array Merge
  • 12.
    // To getpure array from Array like Object
 var pureArray = [].slice.call(object)
 OR
 var pureArray = Array.from(object) NOTE: 1. NodeList from DOM is array of DOM but it don’t have all array protoypes. 2. Arguments from functions are array like object.
 var object = Object.create(Array.prototype);
 
 object.push("foo");
 object.push("bar"); // [ 'foo', 'bar' ]
 
 object.me = "ND"
 
 console.log(object)
 
 /** 
 object
 Array[2]
 0: "foo"
 1: "bar"
 length: 2
 me: "ND"
 __proto__: Array[0]
 **/ Array like Object
  • 13.
  • 14.
    [10] === 10// is false
 [10] == 10 // is true
 '10' == 10 // is true
 '10' === 10 // is false
 [] == 0 // is true
 [] === 0 // is false
 '' == false // is true but true == "a" is false
 '' === false // is false == vs ===
  • 15.
    var arr =["a", "b", "c"]; typeof arr; // return "object"
 arr instanceof Array // true
 arr.constructor(); //[]
 typeof, instanceof & constructor
  • 16.
    (function(a,b){
 var result =a+b;
 return result; 
 })(10,20) OR (function(ng, $){
 ng.module(‘app’, []); 
 })(angular, jQuery) (function(){ 
 // some private code // that will be executed automatically 
 })(); pattern
 step 1. ()()
 step 2. (FN)()
 step 3. (FN)(var1, var2, ...) IIFE- Immediately Invoked Function Expression
 OR 
 Self-Invoked Anonymous Function.
  • 17.
    function callMe(){ 
 var argArray= Array.prototype.slice.call(arguments); 
 conosle.log(argArray) // [ 1, 2, 3, 4, 5 ]
 }
 
 callMe( 1, 2, 3, 4, 5)
 Transform the arguments Object into an Array
  • 18.
    //Get the maxor the min in an array of numbers 
 var numbers = [ 200, 30, -434, 546, 237, 11 ]; 
 var maxInNumbers = Math.max.apply(Math, numbers);
 var minInNumbers = Math.min.apply(Math, numbers); Thank you !! Ohh Sorry...
 Last thing to mention.