JavaScript Secrets
Marko Denic

Marko Denic @denicmarko

About: Software Engineer.

Location:
Austria
Joined:
Nov 16, 2019

JavaScript Secrets

Publish Date: May 4 '20
165 10

In this post I will share with you JavaScript tips you won’t find in most tutorials. Enjoy!

What is JavaScript?

JavaScript is a scripting language that enables you to create dynamically updating content, control multimedia, animate images, and so much more.

Let's jump to the tips!

* Shorten an array

You can set the length property to shorten an array.

Shorten an array

* Short-circuits conditionals

If you have to execute a function only if condition is true, you can use short-circuit.

Short circuit conditionals

* Show specific console.table() columns

By default, console.table() lists all elements in each row. You can use the optional “columns” parameter to select a subset of columns to display:

Show specific console.table() columns

* Remove duplicate elements from the array:

Remove duplicate elements from the array

* Convert a string to number:

Convert a string to number

* Convert a number to string:

Convert a number to string

You can find more HTML/CSS/JS Tips here: github.com/MarkoDenic/awesome-html-css-js-tips

If you liked this article, be sure to ❤️ it.

This article is a repost from my blog. Find the original post here: JavaScript Tips.

Let's keep in touch:
Blog: markodenic.com
Twitter: @denicmarko
Github: github.com/MarkoDenic
Codepen: codepen.io/denic

Comments 10 total

  • dsbarnes
    dsbarnesMay 4, 2020

    Excellent tips. I remember learning how to short circuit through counting substrings of a string like so:

    arr.forEach(e => obj[e] = (ojb[e] || 0) + 1));
    

    Changed my life.

    Never knew about console.table(), that is just awesome.
    Appreciate you sharing

    • Marko Denic
      Marko DenicMay 4, 2020

      Hey @dsbarnes , thanks for reading and taking the time to comment. Glad you like it.

  • Nikola Stojaković
    Nikola StojakovićMay 4, 2020

    Converting a string to a number with a plus sign is a hack and is therefore one of the things you shouldn't do in JavaScript (just like changing the length property of an array).

    • Marko Denic
      Marko DenicMay 4, 2020

      Hi Nikola, thanks for reading. Yes, these two are tricks definitely. But, I saw both on MDN docs and w3docs, so I would consider them a legit tricks. :)

      How do you like the rest?

      • Olga Gnatenko
        Olga GnatenkoMay 5, 2020

        What about explicit conversion from string to number: str => Number(str)? I think this one is clearer and easier to understand/note for other developers.

        • Antonio B.
          Antonio B.May 5, 2020

          parseInt(str, 10)

          • Marko Denic
            Marko DenicMay 5, 2020

            Hi Olga. Hi Antonio.
            Thanks for reading and taking the time to comment.

            There is at least 10 different ways to convert a string to number. Maybe I create a dedicated post about this topic.

  • Peyton McGinnis
    Peyton McGinnisMay 5, 2020

    I actually didn't know some of these. Some great tips!!

    • Marko Denic
      Marko DenicMay 5, 2020

      Thanks Peyton. Glad you like it. :)

  • Myzel394
    Myzel394Nov 23, 2020

    Awesome! Has someone benchmarks for +str and parseInt(str)?

Add comment