Marko Denic

Marko Denic

JavaScript Secrets

JavaScript Secrets

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!

Find the minimum or maximum value in an array

You can use Math.min() or Math.max() combined with the spread operator to find the minimum or maximum value in an array.

const numbers = [6, 8, 1, 3, 9];

console.log(Math.max(...numbers)); // 9
console.log(Math.min(...numbers)); // 1

* 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 an array:

Remove duplicate elements from an array

* Convert a string to number:

Convert a string to number

* Convert a number to string:

Convert a number to string

* Numeric separators

To improve readability for numbers, you can use underscores as separators:

numeric-separators.png

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

 
Share this