16
Javascript Hacks
There are many amazing features in javascript that can make your life much simpler, In this blog, I have added 20 features that every javascript developer should know.
1.Resize the Array using an array. length.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a=['Pune','Hyderabad','Banglore','Mumbai','Indore','Delhi'] | |
console.log(a.length) //OUTPUT 6 | |
a.length=3 | |
console.log(a) //OUTPUT ['Pune','Hyderabad','Banglore'] |
2.Swapping of two Numbers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let a=10; | |
let b=20; | |
console.log(a,b) //OUTPUT: 10,20 | |
[a,b]=[b,a] | |
console.log(a,b) //OUTPUT :20,10 |
3 Concatenating two or more arrays without causing server overload.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//old way | |
a=[1,2,3,4,5] | |
b=[4,5,6,7,8] | |
c=a.concat(b) //This will Create a new array c and then will push contents fo array a and array b in array c which will consume lot of memory. | |
console.log(c) //OUTPUT:1,2,3,4,5,4,5,6,7,8 | |
//new way | |
a=[1,2,3,4,5] | |
b=[4,5,6,7,8] | |
a.push.apply(a,b) // it will only push the content of array b in array a. | |
console.log(a) |
4.Use Filter in a different way.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a=[null,undefined,{"name":"Alex"},{"name":"Nick"},{"name":""},null] | |
c=a.filter(item=>item.name.length>0) //this will show error Uncaught TypeError: Cannot read property 'length' of undefined | |
// we can use filter with Boolean to remove null and undefined Values | |
c=a.filter(Boolean).filter(it=>it.name.length>0) //This will work | |
console.log(c) //OUTPUT |
5.Iterate on the map from 0 to n.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[...Array(100)].map((it,index)=>console.log(index)) |
6.Replace all occurrences of a word in a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
str="India India USA India UK India" | |
console.log(str.replace(/India/,'USA')) | |
//OUPUT USA India USA India UK India | |
//Add g at the end of string it will replace all occurences | |
console.log(str.replace(/India/g,'USA')) |
7.Shortcut for conditions.
Here, I am explaining basic two examples we can implement in multiple ways.
Here, I am explaining basic two examples we can implement in multiple ways.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// old way | |
let a="hello" | |
let b; | |
if(a==undefined) | |
{ | |
b="Nothing" | |
} | |
else | |
{ | |
b=a | |
} | |
console.log(b) //hello | |
//new way | |
b=a||"Nothing" | |
console.log(b) //hello | |
// old way | |
let data={"name":"Allen"} | |
if(data!=undefined)console.log(data.name) | |
// new way | |
data!=undefined&&console.log(data.name) |
8.String to number / Number to string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// string to number | |
a="123" | |
console.log(+a) //Output 123 | |
b="" | |
console.log(+b) //NaN | |
//number to string | |
a=123 | |
console.log(a+"") |
9.Use the console in different ways.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// %s replaces an element with a string | |
console.log("Helllo I love %s","Javascript") | |
// %d replaces an element with an integer | |
console.log("Helllo %d ",1) | |
// %f replaces an element with a float | |
console.log("Helllo %f ",1.078) | |
// %(o|O) | element is displayed as an object. | |
console.log("Helllo %O",{"Name":"Sidd"}) | |
// %c | Applies the provided CSS | |
console.log("%cThis is a red text", "color:red"); |
10.Use console.table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// we can use console.table to show objects in tabular format | |
a=[{"city":"Banglore"},{"city":"Pune"},{"city":"Mumbai"}] | |
console.table(a) |
11.Get the items near to last index of an array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a=[1,2,3,4,5,6,7,8,9,10] | |
conosle.log(a.slice(-1))//[10] | |
console.log(a.slice(-2))//[9,10] |
12.Get n power of any number.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log(2 ** 3) //8 | |
console.log(2 ** 12) //4096 |
13.Check the falsy or truly value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//In javascript following values are falsy 0, "", null, undefined, NaN and of course false except it all are truly | |
// use !! operator to get falsy or trult | |
console.log(!!0,!! "",!!null,!! "Hii",!! undefined,!! NaN ,!! false,!! true) | |
//OUTPUT false false false true false false false true |
14.Call a function by its name stored in a string using eval function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const print=()=>console.log("hello") | |
setTimeout(()=>eval('print')(),1000)//hello |
15.typeof Operator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The typeof operator returns a string indicating the type of the unevaluated operand. | |
console.log(typeof 12) //number | |
console.log(typeof "hello") //string | |
console.log(typeof []) //object | |
console.log(typeof true) //boolean | |
console.log(typeof {}) //object |
16.yeild keyword
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The yield keyword is used to pause and resume a generator function | |
function* list(index,length) { | |
while (index < length) { | |
yield index; | |
index++; | |
} | |
} | |
const iterator = list(0,10); | |
console.log(iterator.next().value); | |
// expected output: 0 | |
console.log(iterator.next().value); | |
// expected output: 1 |
17.function* in javascript.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The function* declaration (function keyword followed by an asterisk) defines a generator function, which returns a Generator object. | |
function* generator(i) { | |
yield i; | |
yield i + 10; | |
} | |
const gen = generator(10); | |
console.log(gen.next().value); | |
// expected output: 10 | |
console.log(gen.next().value); | |
// expected output: 20 |
18.new.target in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// new.target is used to detect that weather a function or constructor call using new or not. | |
// if it called using new it will return refrence to constructor or function else it return undefined. | |
function learn(){ | |
new.target?console.log("Called using new"):console.log("Called without new") | |
} | |
learn() //called without learn | |
new learn() //called using new. | |
//In arrow functions, new.target is inherited from the surrounding scope. |
19.label statement.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The labeled statement can be used with break or continue statements. It is prefixing a statement with an identifier which you can refer to. | |
loop1: | |
for (let i = 0; i < 5; i++) { | |
loop2: | |
for(let j=0;j<5;j++) | |
{ | |
console.log(i,j) | |
if(i==j) | |
{ | |
continue loop1 | |
} | |
} | |
} | |
// 0 0 | |
// 1 0 | |
// 1 1 | |
// 2 0 | |
// 2 1 | |
// 2 2 | |
// 3 0 | |
// 3 1 | |
// 3 2 | |
// 3 3 | |
// 4 0 | |
// 4 1 | |
// 4 2 | |
// 4 3 | |
// 4 4 |
20.Rest parameters Syntax.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The rest parameter syntax allows a function to accept an indefinite number of arguments as an array,it like variadic function in C. | |
function abc(...args) | |
{ | |
console.log(args) | |
} | |
abc(1) //[1] | |
abc(1,2) //[1,2] | |
abc(1,2,3) //[1,2,3] | |
abc(1,2,3,4)//[1,2,3,4[ |
Conclusion:
Thanks for reading it, In this blog I have explained some of the most used features of javascript, I will explain more features in next blog,
Please give feedback in responses.
Thanks for reading it, In this blog I have explained some of the most used features of javascript, I will explain more features in next blog,
Please give feedback in responses.
16