DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Ren Hiyama
Ren Hiyama

Posted on • Updated on

Make it Short - Make it Better

Hey there Fellow Javascript Coder!
Whether you're a experienced programmer or just someone who just started with javascript basics, this list of hacks is definitely for you! Please note that you might have already known one or more features of javascript from the below list, and that's awesome! But not everyone knows them πŸ˜‰

So let's straight hop in!

Let's start


Convert Any Datatype to Boolean

By using !! in front of any variable, we can get the Boolean version of it! Let's see how:

let variable = 100;
variable = !!variable // returns true
variable = 0;
variable = !!variable //returns false
Enter fullscreen mode Exit fullscreen mode

Isn't this easier to write and use in real life?
Well, this is more easier:

let variable = 100;
if(variable) console.log("True"); // Logs True!
variable = 0;
if(variable) console.log("True"); // doesn't log anything
Enter fullscreen mode Exit fullscreen mode

Find whether a property exists in an Object

If you were using if(obj.property) or if(obj["property"]) till now, it's time to make your code more readable by using if("property" in obj).

Become Lazy and truncate Array just as you wanted

This just works!

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(arr); // Logs all the 10 elements
arr.length = 5;
console.log(arr); // Logs only [1, 2, 3, 4, 5] Holy Moly!?
Enter fullscreen mode Exit fullscreen mode

Remove Duplicates from your Array

let arr = [1, 2, 1];
console.log(arr); // [1, 2, 1], as expected.
arr = Array.from(new Set(arr));
console.log(arr); // [1, 2], and cheers!
Enter fullscreen mode Exit fullscreen mode

Avoid Server Lag during 2 or more Array Concats

Instead of using this:

let list1 = ['a', 'b', 'c', 'd', 'e'];
let list2 = ['f', 'g', 'h', 'i', 'j'];

let list = list1.concat(list2);
Enter fullscreen mode Exit fullscreen mode

Try using this alternative:

let list1 = ['a', 'b', 'c', 'd', 'e'];
let list2 = ['f', 'g', 'h', 'i', 'j'];

let list = list1.push.apply(list1, list2)
Enter fullscreen mode Exit fullscreen mode

The latter code saves your server from high memory consumption because the first code creates a new array in memory, whereas this one works with the list1 array instead of creating a new array.


I hope I made you learn something new today! If I did, consider hitting a like, a unicorn and a bookmark on this Post! Also you can hit a star on my current project - Reejs at https://github.com/rovelstars/reejs , a star means a lot to me!

Have a nice day πŸ™ƒ!

P.S. Comment down below if you have any ideas on what the next blog should be, I will try my best if I can write on it!

Top comments (13)

Collapse
 
lukeshiru profile image
Luke Shiru

short !== better if you sacrifice readability or rely in hacky code...

  • Almost every time you want to "convert to boolean", what you actually want to do is check for the "trhuthiness" of a value, so instead of doing something like !! you should either use the value directly, or compare it to what you want to avoid:
if (number) {
  // number is different from 0
} else {
  // number is 0
}

// or more clear
if (number !== 0) {
  // number is different from 0
} else {
  // number is 0
}
Enter fullscreen mode Exit fullscreen mode

For other scenarios in which number might be nullish, you can use ?? as well. But long story short is that if you have !! in your code, you should take it as a red flag.

  • For array "truncation", you should avoid mutating the array's length, and instead just use Array.prototype.slice. Mutations would end up biting you if you're not careful with them.
  • To remove duplicates you can actually make it "shorter" and is still readable: [...new Set(array)].
  • The last example doesn't make any sense, and not only that, it doesn't follow the topic of the post itself. You can use Array.prototype.concat or just spread to concat: [...array1, ...array2], no need to apply push (which would also mutate the original array).

Cheers!

Collapse
 
thecodingcrow profile image
thecodingcrow

very much agree on the whole array part! unnoticed mutations can be such a pain in the ass!

Collapse
 
renhiyama profile image
Ren Hiyama

Reminds me of GIGO - Garbage In Garbage Out πŸ™ƒ

Collapse
 
renhiyama profile image
Ren Hiyama

Thanks for the information! Will try to keep the next blog better!

Collapse
 
tracygjg profile image
Tracy Gilmore

Hi Ren, An alternative to if(variable) console.log("True"); can be found as a method in the Console API., although the logic needs to be inverted.

console.assert expects at least two parameters; a Boolean assertion and a message (or any other data) you want to present.

So the alternative to the above code is console.assert(!variable, "True"). It is typically used to flag up when something went wrong but I have never seen it used in production code.

Collapse
 
renhiyama profile image
Ren Hiyama

You probably didnt see it in production code because many people know what assert does, including me! I just use console.log during testing and remove them when their logging work for testing is over

Collapse
 
naubit profile image
Al - Naubit

Thanks, it was a good read, bookmarked, and followed!

Collapse
 
mrcaidev profile image
Yuwang Cai

if (obj.property) does not equal if (property in obj) when obj.property is falsy. And I really think arr.length should be considered immutable. Good point though!

Collapse
 
devangtomar profile image
Devang Tomar

That was a nice read! Liked, bookmarked and followed, keep the good work! πŸ™Œ

Collapse
 
renhiyama profile image
Ren Hiyama

Thanks πŸ‘ you can even star my current project github.com/rovelstars/reejs for support πŸ™ƒ

Collapse
 
erfannvb profile image
Erfan • Edited on

No offense, but I don't think these codes are going to be useful

Collapse
 
renhiyama profile image
Ren Hiyama

No problem! I will try my best to write better blogs!

Collapse
 
awcode0x profile image
AWCode0X

Good Work πŸ‘πŸ‘

An Animated Guide to Node.js Event Lop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.