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 javascri…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Ren Hiyama

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

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

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!?

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!

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);

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)

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!


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Ren Hiyama


Print Share Comment Cite Upload Translate Updates
APA

Ren Hiyama | Sciencx (2022-09-10T18:40:22+00:00) Make it Short – Make it Better. Retrieved from https://www.scien.cx/2022/09/10/make-it-short-make-it-better/

MLA
" » Make it Short – Make it Better." Ren Hiyama | Sciencx - Saturday September 10, 2022, https://www.scien.cx/2022/09/10/make-it-short-make-it-better/
HARVARD
Ren Hiyama | Sciencx Saturday September 10, 2022 » Make it Short – Make it Better., viewed ,<https://www.scien.cx/2022/09/10/make-it-short-make-it-better/>
VANCOUVER
Ren Hiyama | Sciencx - » Make it Short – Make it Better. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/10/make-it-short-make-it-better/
CHICAGO
" » Make it Short – Make it Better." Ren Hiyama | Sciencx - Accessed . https://www.scien.cx/2022/09/10/make-it-short-make-it-better/
IEEE
" » Make it Short – Make it Better." Ren Hiyama | Sciencx [Online]. Available: https://www.scien.cx/2022/09/10/make-it-short-make-it-better/. [Accessed: ]
rf:citation
» Make it Short – Make it Better | Ren Hiyama | Sciencx | https://www.scien.cx/2022/09/10/make-it-short-make-it-better/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.