10 Magical JavaScript Tips for Every Web Developer

This article will cover 10 JavaScript tips that web developers can use to save time and money.

Although I don’t always enjoy being taught, I am always willing to learn.

Winston Churchill

Flatten the array

This tip will allow you to flatten an array with many nested elements by using Infinity in Flat.

var array = [123, 500, [1, 2, [34, 56, 67, [234, 1245], 900]], 845, [30257]]//flatten array of array
array.flat(Infinity)// output:
// [123, 500, 1, 2, 34, 56, 67, 234, 1245, 900, 845, 30257]

Simple Exchange Variables

The third variable temp will swap the variables. This tip will help you to use destructuring to swap variables.

//example 1var a = 6;
var b = 7;[a,b] = [b,a]console.log(a,b) // 7 6

Sort alphabetically

Programming can be difficult when it comes to sorting strings alphabetically. This tip will help you save time and cut down on your programming efforts.

//sort alphabeticallyfunction alphabetSort(arr)
{
return arr.sort((a, b) => a.localeCompare(b));
}let array = ["d", "c", "b", "a"]
console.log(alphabetSort(array)) // ["a", "b", "c", "d"]

Create a range of numbers

Let’s say you need to generate a number within a certain range. You will first use the loop. This tip will help you save valuable time and make it easy.

let Start = 1000, End = 1500;
//Generating
[...new Array(End + 1).keys()].slice(Start);Array.from({length: End - Start + 1}, (_,i) => Start + i) // [1000, 1001 .... 1500]

Leave the Array empty

This tip will help you save time when emptying an array. This is the JavaScript length method that I’ll show you to empty an array.

let array = ["A", "B", "C", "D", "E", "F"]array.length=0
console.log(array) // []

Use isString

This tip will help you check if data or values are in string format. This is useful when you need to verify the data type of data that has been requested from the server.

const isString = vaue => typeof value === 'string';isString('JavaScript'); // true
isString(345); // false
isString(true); // false

Check Null

Sometimes, programming can require us to verify that a result or piece of data is valid.

const CheckNull= value => value === null || value === undefined;CheckNull(null) // true
CheckNull() // true
CheckNull(123) // false
CheckNull("J") // false

Combine Arrays into One

This tip is useful when you want to combine two arrays of any size. This can be done using the JavaScript concate technique.

//Example Code
let arr1 = ["JavaScript", "Python", "C++"]
let arr2 = ["Dart", "Java", "C#"]const mergeArr = arr1.concat(arr2)
console.log(mergeArr) // ["JavaScript", "Python", "C++", "Dart", "Java", "C#"]

>> Backend Developer Roadmap

Performance Quick Calculation

This tip is most used by me to calculate the performance my JavaScript program.

const starttime = performance.now();
//some program
const endtime = performance.now();const totaltime = startTime - endTime
console.log("function takes "+totaltime +" milisecond");

Eliminate duplicates

It is likely that you will encounter arrays with duplicate data. To get rid of them, use a loop method. This tip will allow you to remove duplicates quickly and without any loop.

const ReDuplicates = array => [...new Set(array)];console.log(ReDuplicates([200,200,300,300,400,500,600,600])) // [200,300,400,600]

Last Thoughts

These tips are useful and enjoyable to me. Share your JavaScript tips with others if you have one.

Share this article with friends if you found it useful.

Happy Coding….☺️

About us: Codelivly is a platform designed to help newbie developers to find the proper guide and connect to training from basics to advance.

60 Comments

  1. Great delivery. Outstanding arguments. Keep up
    the amazing effort.

  2. This is halrious …
    Thanks man for this

  3. Thanks again for the article post. Really Cool.

  4. Have you ever thought about adding a little bit more than just your articles?
    I mean, what you say is fundamental and everything. But just imagine if you added some great photos or video clips
    to give your posts more, “pop”! Your content
    is excellent but with pics and videos, this website could undeniably be one of the greatest in its field.
    Fantastic blog!

  5. What i do not understood is if truth be told how you are now not really much more neatly-preferred than you may be now.
    You’re very intelligent. You know thus significantly in relation to this subject, made me
    personally believe it from numerous various angles. Its like women and men are not fascinated except it is one thing to do with
    Lady gaga! Your personal stuffs great. Always maintain it up!

  6. I’m more than happy to discover this website. I wanted to thank you for ones time for this particularly fantastic read!!
    I definitely loved every little bit of it and i also have you book marked to check out new
    stuff in your web site.

  7. Pretty portion of content. I just stumbled upon your site and
    in accession capital to claim that I acquire actually loved account your
    weblog posts. Anyway I’ll be subscribing for your feeds or even I success you
    get admission to persistently fast.

  8. Hello there, You’ve done a fantastic job. I will
    certainly digg it and personally suggest to my friends.
    I’m confident they’ll be benefited from this site.

  9. I have been exploring for a bit for any high quality articles
    or blog posts on this sort of space . Exploring in Yahoo I finally stumbled upon this site.
    Studying this information So i’m happy to exhibit that
    I’ve an incredibly just right uncanny feeling I found out just what I needed.
    I most definitely will make sure to do not forget this web site
    and provides it a glance regularly.

  10. Thanks a bunch for sharing this with all people you
    actually recognise what you are talking approximately! Bookmarked.

  11. What’s up, just wanted to tell you, I loved this article.
    It was funny. Keep on posting!

  12. Wow that was strange. I just wrote an incredibly long comment but after I clicked
    submit my comment didn’t show up. Grrrr… well I’m not
    writing all that over again. Anyhow, just wanted to say
    superb blog!

  13. Hello, for all time i used to check web site posts here early
    in the daylight, since i enjoy to find out more and
    more.

  14. Ahaa, its pleasant dialogue regarding this piece of writing
    here at this website, I have read all that, so now me also commenting here.

  15. This site was… how do I say it? Relevant!! Finally I have found something which helped me.
    Appreciate it!

  16. Hi there everyone, it’s my first pay a visit at this web page, and post is really fruitful
    for me, keep up posting these types of articles.

  17. I got this web page from my buddy who informed me about this web site and now
    this time I am browsing this website and reading very informative articles or
    reviews here.

  18. What’s up, this weekend is good designed for me, because
    this point in time i am reading this fantastic educational
    article here at my residence.

  19. I like the helpful info you supply on your articles.
    I’ll bookmark your blog and check once more here regularly.
    I’m rather certain I will be informed plenty of new stuff proper here!
    Good luck for the following!

  20. When someone writes an paragraph he/she retains the thought of a user in his/her brain that how a user can be
    aware of it. Thus that’s why this post is great.

    Thanks!

  21. This piece of writing provides clear idea designed for the new visitors of blogging, that actually how to do blogging and site-building.

  22. I visited various web sites however the audio quality for
    audio songs current at this site is in fact marvelous.

  23. Pretty nice post. I simply stumbled upon your weblog and wanted to say that I have
    really loved browsing your weblog posts. In any case I’ll be subscribing in your rss feed and I am hoping you write once
    more very soon!

  24. It’s hard tto come by experiennced people
    for this subject, however, you sound like you know what you’re talking about!
    Thanks

  25. I am regular visitor, how are you everybody?

    This piece of writing posted at this web site is truly fastidious.

  26. If you desire to grow your experience simply keep visiting this web page and be updated with the latest news posted here.

  27. It’s difficult to find knowledgeable people in this particular subject, but you seem like
    you know what you’re talking about! Thanks

  28. Hiya! Quick question that’s completely off topic.
    Do you know how to make your site mobile friendly? My web site
    looks weird when browsing from my iphone 4. I’m trying to find a
    theme or plugin that might be able to resolve this issue.
    If you have any suggestions, please share. Thank you!

  29. Remarkable things here. I am very happy to peer your post.
    Thank you so much and I am having a look forward
    to contact you. Will you kindly drop me a mail?

  30. Hi, I do think this is a great web site. I stumbledupon it 😉
    I will revisit yet again since i have saved as a favorite it.
    Money and freedom is the best way to change, may you be rich and continue
    to help others.

  31. I get pleasure from, result in I discovered exactly what I used to be looking for.
    You’ve ended my 4 day lengthy hunt! God Bless you man. Have a nice day.
    Bye

  32. Howdy exceptional blog! Does running a blog like this take a massive amount work?
    I’ve absolutely no understanding of coding but I was hoping to start my
    own blog soon. Anyways, should you have any recommendations or tips for
    new blog owners please share. I know this is off topic nevertheless I simply
    wanted to ask. Thanks!

  33. Good response in return of this matter with firm arguments and describing all regarding that.

  34. Hi it’s me, I am also visiting this website daily, this site is really nice and the users are truly sharing fastidious thoughts.

  35. Hello, after reading this amazing post i am
    too happy to share my experience here with mates.

  36. I do not even know how I ended up here, but I thought this post
    was good. I don’t know who you are but certainly you’re going to a
    famous blogger if you are not already 😉 Cheers!

  37. Pretty great post. I simply stumbled upon your blog
    and wanted to mention that I have really enjoyed browsing your blog posts.
    In any case I will be subscribing to your rss feed and I
    am hoping you write again soon!

  38. What’s up to every one, it’s truly a good for me to go to see this website, it contains valuable Information.

  39. Good post. I learn something totally new and challenging on blogs I stumbleupon every day.

    It’s always interesting to read through articles from
    other authors and use something from their sites.

  40. My spouse and I stumbled over here by a different web page and thought I might check things out.
    I like what I see so i am just following you. Look forward to going
    over your web page for a second time.

  41. It’s going to be ending of mine day, however before finish
    I am reading this great paragraph to increase my knowledge.

  42. What a material of un-ambiguity and preserveness of valuable knowledge concerning unexpected feelings.

  43. Generally I don’t read post on blogs, however I wish to say that this write-up very forced me to check
    out and do it! Your writing style has been surprised me.
    Thanks, quite great post.

  44. Way cool! Some very valid points! I appreciate you penning this post plus the rest of the site is very good.

  45. I am in fact thankful to the owner of this web page who has shared this
    great post at here.

  46. I’ve been exploring for a little bit for any high-quality articles or blog posts on this
    kind of house . Exploring in Yahoo I finally stumbled upon this website.
    Studying this info So i am satisfied to show that I have
    a very just right uncanny feeling I found out exactly
    what I needed. I such a lot undoubtedly will make sure to don?t omit this site and give
    it a look regularly.

  47. Great blog here! Also your website loads up very fast!

    What web host are you using? Can I get your affiliate link to your
    host? I wish my site loaded up as quickly as yours lol

  48. For newest information you have to pay a quick visit internet and on internet I found this
    website as a most excellent web page for hottest updates.

  49. Howdy very nice web site!! Man .. Beautiful .. Superb ..
    I’ll bookmark your site and take the feeds also? I’m satisfied to find numerous
    useful info right here in the put up, we want develop more
    strategies on this regard, thank you for sharing. . . . .

    .

  50. Thanks for the marvelous posting! I seriously enjoyed reading it,
    you will be a great author.I will be sure to bookmark your blog
    and will eventually come back very soon. I want to encourage you to continue your great work, have
    a nice morning!

  51. Hey! Do you know if they make any plugins to help with Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success.
    If you know of any please share. Cheers!

  52. I am genuinely thankful to the owner of this web site who has shared this great paragraph at here.

  53. My brother suggested I might like this web site. He was totally right.
    This post actually made my day. You can not imagine simply how much time I had spent for this information!
    Thanks!

  54. Very soon this web site will be famous among all blogging users, due to it’s fastidious articles

  55. Quality posts is the crucial to attract the visitors to pay a visit the web
    site, that’s what this web page is providing.