Today I am going to share 5 tricks to use when you write javascript code for the ease of your coding.
1. Remove falsy values from an array
In our coding journey, we often have to use an array. Suppose you have an array that consists of truthy and falsy values but you need only truthy values from the array. How will you do that? I know you are thinking about the looping process but without a loop, we can simply get truthy values from the array.
// An array consists with truthy and falsy values
let array = ['t', 0, undefined, 'r', null, 'u', '', false, NaN,'e']
array = array.filter(Boolean)
console.log(array);
//Output will be
[ 't', 'r', 'u', 'e' ]
2. Convert any value to a boolean
We often need boolean values in javascript. For that purpose, if you want to make any value boolean, you can make it by using two exclamatory signs.
let a = !!'Boolean'
let b = !!""
let c = !!undefined
console.log(a,b,c)
//Output will be
true false false
3. Flatten multi-dimensional array
We can have a scenario where we need to convert a multi-dimensional array to a single array which will consist of all the values of the array. Let's say we have an array,
let array = ['javascript', ['python', 'go'], 'c', ['c++','#c'],'rust']
We can convert the array into a single array by using a method called Array.prototype.flat() which is
let array = ['javascript', ['python', 'go'], 'c', ['c++','#c'],'rust']
array = array.flat()
console.log(array)
//Output will be
['javascript', 'paython', 'go', 'c', 'c++', '#c', 'rust']
But there is a problem with the method you can not convert the multi-dimensional array to a single array if any of the inner values contains another array. Like,
let array = ['javascript', ['python', 'go'], 'c', ['c++','#c', ['ruby', 'swift']],'rust']
To solve this, all you need to use is,
let array = ['javascript', ['python', 'go'], 'c', ['c++','#c', ['ruby', 'swift']],'rust']
array = array.flat(Infinity)
console.log(array)
//Output will be
['javascript', 'paython', 'go', 'c', 'c++', '#c','ruby', 'swift', 'rust']
4.Replace any specific word from a string
Let's say you have bigger texts and you want to replace a specific word from the whole string. We can easily do it by using the following method,
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replaceAll('dog', 'monkey'));
// Expected output: "The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?"
5.Short conditionals
For using conditions we often use if else conditions. We can easily write our conditions without using if else.
let a = 'Javascript'
if(typeof a ==='string'){
console.log('Welcome the realm of javascript')
}
else{
console.log('Go, learn some javascript')
}
//Instead of using this way, we can log the message by short conditionals
typeof a==='string' ? console.log('Welcome the realm of javascript') : console.log('Go, learn some javascript')
That's all for today. I will try to come up with new tricks and tips very soon.