10 Javascript Neat Tricks That You Probably Did Not Know
Did you know that there are snippets of hidden JavaScript tricks to make your job easier? In this blog, we are going to discuss these 10 hidden JavaScript tricks. So without wasting any more time , let’s do it let’s get started.
1. Shortening console.log
Developers always like to save time, but writing console.log
, again and again, sometimes it's tedious. But this next trick will show you how To minify your console.log file, see the code snippet below.
var a = console.log.bind(document)
a(3*4)
a("Welcome to YourQuorum")
a(true)
2. Smart Functions
Arrow functions are one of the functions introduced in ES6 JavaScript and are mainly used to implement short functions and to make the code cleaner and easier to read compared to regular functions. Below is sample code for using arrow functions.
// Smart Function (Arrow fun)
function yourquorum(str){
console.log(str)
}
// Arrow Functions
let yourquorum2 = (str) => { console.log(str) }
yourquorum("Sign in Yourquorum")
yourquorum2("Create a question")
Read Also : How JavaScript Works: An Overview Of The Engine, The Runtime, And The Call Stack?
3. Short Conditions
Instead of writing multiple lines of conditional code, you can write it in a single line. This hack shows you how to do it. This is useful when working in multiple conditions at the same time.
// Short Conditions
var condtion = true
if(condtion)
{
console.log("Login Successful")
}
// Short condtion
condtion && console.log("Login Successful")
4. Flat Multi-dimension Array
This hack will help you flatten matrices in a way, see code snippet below.
//deep flatten
var array = [100, [200, [300, 400, 500], 600], [700, 800,[900,[1000]]]]
console.log(array.flat(Infinity)) // [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
You may also like : Top 10 tricky JavaScript questions that I used to ask in interviews
5. console.table()
This awesome trick will help you convert your data from CSV or dictionary format to spreadsheet format using the console.table() method.
// Console.table
var data=[{"category":"Discussion"}, {"category":"Health"}, {"category":"Education"}]
console.table(data)
6. Multiple Replace
We know we can use the replace() method to replace words in a string, but what about multiple replaced words in the same string? This trick will help you do it with the replace() method by adding a /g keyword at the end. Look at the following code to understand it.
// Multiple Replace
var data = "JavaScript is JavaScript"
//Single
console.log(data.replace(/JavaScript/, "TypeScript")) // TypeScript is JavaScript
//Multiple
console.log(data.replace(/JavaScript/g, "TypeScript")) // TypeScript is TypeScript
7. Number and String Conversion
Conversion is a part of every programming language and in JavaScript you can easily convert numbers to strings and strings to numbers. See the code below.
// Number to String
var data1 = 200
data1 = data1 + ""
console.log(data1) // 200
console.log(typeof(data1)) //String
// String to number
var data2 = "200"
data2 =+ data2
console.log(typeof(data2)) //number
8. Infinite Parameter in a Function
Suppose you passed 100 arguments in the function and now you have a problem to set 100 parameters in the function implementation. This problem is solved by this trick which shows you how to use remainder parameters to cover the infinite definition of parameters in the function.
// Infinite function parameter
function fun(...data)
{
for(let i of data)
{
console.log(i) // 1 3 4 5 6
}
}
fun(1 ,3, 4, 5, 6)
9. Shuffle an Array
Array shuffling can be done in an easy way by following the example code. This mostly comes in handy when you’re working on some game project in JavaScript and you need to shuffle your array.
// Shuffle An Array
var array = [100, 200, 300];
console.log(array.sort(function()
{
return Math.random() - 0.5
}));
// [100, 300, 200]
10. Get the Last Item in an Array
This tip This saves you from iterating over the whole array to get the last element.In the code example below, we are using an array slice method with a negative number -1 that takes the elements from the last.
// Get last element of Array
let array = [100, 200, 300, 400, 500]
console.log(array.slice(-1)) // [500]
console.log(array.slice(-2)) // [400, 500]
FAQ: