Prototypr

Prototyping, UX Design, Front-end Development and Beyond 👾 | ✍️ Write for us https://bit.ly/apply-prototypr

Follow publication

5 Must-know Javascript Tips & Tricks

Do you know them all?

Twan Mulder
Prototypr
Published in
3 min readJul 13, 2020

--

JavaScript keeps adding new and neat features. Sometimes, it’s hard to keep up. In this article, I’ll share a couple of cool tips & tricks to keep you up to speed and deepen your JS knowledge.

1. Create an array with unique values using the “Set” object

Imagine having an array with some duplicate items and wanting to filter out only the unique ones.

You could try writing a map or filter to achieve this. Alternatively, ES6 introduces the Set object, which solves this problem in just 1 line of code.

const arrayWithUniqueItems = [...new Set([1, 2, 3, 3,])]
// [1, 2, 3]

Now, this example uses integers, but you can use strings and floating-point numbers as well!

For a little more in-depth knowledge about the Set object, check out this article by Claire-Parker Jones.

2. Shorten your “if” statements

Now this is a tricky one.

Shortening your “if” statements can be a great way to simplify your code.

However, if you need to write more complicated statements, you should definitely go for the first option.

// Instead of using this                                      
if (iAmHungry) {
bakeAnEgg()
}
// You can use this
if (iAmHungry) bakeAnEgg()
// Or this
iAmHungry? bakeAnEgg() : 0

Remember, readability & ease-of-use are more important than a couple less lines of code.

3. Shorten an array using its length property

--

--

Published in Prototypr

Prototyping, UX Design, Front-end Development and Beyond 👾 | ✍️ Write for us https://bit.ly/apply-prototypr

Written by Twan Mulder

Frontend Developer who loves creating enticing experiences | Employed at De Bijenkorf

Responses (6)

Write a response