Home / JavaScript / For Beginners, Here Are 5 Useful JavaScript Tricks
3 minutes read
110 Views
Blog post thmbnai js 2

I’ll teach you 5 great javascript tricks that will make your life easier in this post. And it will assist you in becoming a better programmer. Continue reading if you’re interested.

Trick#1: Get rid of duplicates in an array!

This is a very basic trick. Assume I have an array with integers, strings, and booleans in it. And I want to make sure there are no duplicate items in this array. So, how do you go about doing that?

const array = [1, 2, 3, 2, 1, true, true, false, 'Ratul', 1, 5];
const filtered__array = [...new Set(array)];
console.log(filtered__array) // [ 1, 2, 3, true, false, 'Ratul', 5 ]

It is Simple!

Trick#2: Convert a decimal to an integer.

This is a really simple technique to perform. Allow me to demonstrate.

const number = 23.6565
console.log(number | 0);

Is not it also so simple!

Trick#3: Getting an Array’s Last Value!

Assume you have a collection of something. Now, how are you going to get the array last item?

const array = [1, 2, 3, 4, 5]
const last_Item = array.slice(-1)
console.log(last_Item)

Let’s get started! Now, if you type -2 instead of -1, you’ll get the array’s last two values, and if you type -3 instead of -2, you’ll get the value of the array’s last three indexes, and so on.

Trick#4: From an array, get a random index value.

Let’s pretend we’re running a lottery. We have an array with the names of the participants in it. Now we simply want one person from the array to choose a winner at random.

const participants = ['Ratul', 'George', 'july', 'Padrik', 'G']
const winner = participants[Math.floor(Math.random() * participants.length)]
console.log(winner) // july was the winner 😊

Trick#5: Identify the longest word in an array.

Make an array and fill it with various strings. Print the longest string in this array now.

const array = ['Apple', 'Pine-apple', 'Banana', 'Jack-fruit']

let most_lengthy_string = ''
array.forEach((item) => {
  if (item.length > most_lengthy_string.length) {
    most_lengthy_string = item
  }
})
console.log(most_lengthy_string)

Simple! So, let me to clarify what’s going on. To begin, we have an array that contains some strings. Following that, I established a variable that contains an empty string. Now, in order to find the longest string in this array, I must examine all of the array entries. So I’ve gone through the array in a loop. We are reassigning the value of the variable if the array’s item length is more than the length of our “most lengthy string.” After all, I am only writing out the variable. That’s it!

Conclusion

Thank you for taking the time to read this article. I hope you found it entertaining. Please let me know if you have any questions about that post. Also, make sure you’re following me on Twitter to receive all of my informative postings.

Read More:


Post author

A bright, talented, ambitious and self-motivated 💻 Freelancer, ⌨ Programmer, JavaScript and Typescript lover 📍

  Tweet It


Leave a Reply

Your email address will not be published. Required fields are marked *