DEV Community

Cover image for 12 javascript pro snippet codes for everyday problems😎😎
Ashish donga
Ashish donga

Posted on • Updated on

12 javascript pro snippet codes for everyday problems😎😎

level up your javascript skills with these pro snippet codes

in this post i will share 12 javascript snippet codes for your daily life probelm. it will be your toolbox that you can freely use in your javascript project

1. Search Object in Array

you can search an object in array using js find() method below is snippet code example

// search object in array
let data = [
    {name:"pizza",salary:60000},
    {name:"burger",salary:30000},
    {name:"sweet",salary:20000}
]

let emp = data.find(data => data.name === 'pizza')
console.log(emp)
Enter fullscreen mode Exit fullscreen mode
//output
{
    name:"pizza",
    salary:60000
}
Enter fullscreen mode Exit fullscreen mode

2. String Reversing

this snippet code will show you how to reverse a string without using a loop

var reverse = (data) => {
    return 
    data.split("").reverse().join("");
}

console.log(reverse('CoderBoy'))
console.log(reverse('Medium'))
Enter fullscreen mode Exit fullscreen mode

3. concatenate lists

now you don't need to use functions and loops to merge many lists into one. you can use js built in concat() method. check out the below code example.

let arr1 = [10,20,30]
let arr2 = [40,50]
var arr = arr1.concat(arr2)
console.log(arr) // [10,20,30,40,50]
Enter fullscreen mode Exit fullscreen mode

4. capture right click

this simple snippet code will capture the right click of th mouse on a web browser

window.oncontextmenu = () => {
    console.log("right click i pressed!")
}
Enter fullscreen mode Exit fullscreen mode

5. smart data filteration

filter your data with javascript built in filter method this comes in handy when you had a large amount of data in form and want to filter some elements from it

var data = ["football","soccer","cricket","basketball","hockey"]
var filterd_data = data.filter(data => data.length < 8)
console.log(filterd_data)
Enter fullscreen mode Exit fullscreen mode

6. looping keys and values

another useful snippet code to iterate keys and values of dictionary data. we are going to use the forEach method for this task

let data = { javascript:1, Dart:2, Java:3};
Object.keys(data).forEach((key,values) => {
    console.log(keys,values)
})

Enter fullscreen mode Exit fullscreen mode
//output 
javascript 1
Dart 2
Java 3
Enter fullscreen mode Exit fullscreen mode

7. find index of array element

now you don't need to iterate the whole array to find an index of any element. make your life easier by taking a loop at the below snippet code

var num = [9,8,4,2,8,0,3,8]
num.sort()
console.log(num)
Enter fullscreen mode Exit fullscreen mode

8. check string is uppercase

this is a simple snippet that will help you to check whether a sting is uppercase or lowercase

const checkUpper = string => string === string.toUpperCase();
console.log(checkUpper("Hello")) // false
console.log(checkUpper("LEARN")) // true
Enter fullscreen mode Exit fullscreen mode

9. built in sorting

sorting is a common problem og every programming language. in javascript you can use the sort() method to sort any list of elements

var num = [9,8,4,2,8,0,3,8]
num.sort()
console.log(num)
Enter fullscreen mode Exit fullscreen mode

10. handle errors in js

error is always a headache in programming to handle errors in javascript you can use the try/catch statment. checkout the below syntax

//error handling
try{
    //code block to try
}
catch{
    //code block to handle error
}
finally{
    //code block to be executed regardless of the try and catch results
}
Enter fullscreen mode Exit fullscreen mode

11. Destructing assignment

you can use destructing method to unpack the array values and assign them to other variables. check out the below example code

let data = ["haider","22","50000","web developer"]
let [name,age,salary,profession] = data
console.log(name,age,salary,profession)
Enter fullscreen mode Exit fullscreen mode

12. slicing array

this is another snippet code that will slice your array without using any loop. the syntax for slice is slice(startIndex,endIndex)

let array = [10,12,13,14,15,16]
console.array(array.slice(0,3))//[10,12,13]
console.array(array.slice(0,2))//[10,12]
Enter fullscreen mode Exit fullscreen mode

What is difference between undefined & null in javascript

Top 7 best Algorithms to Improve your JavaScript Skills

7 killer JavaScript One-Liners You Must Know

final thoughts

well that is for this 12 javascript pro snippet codes article i hope you find this article helpful and fun to read, feel free to share your valuable response and don't forget to share this article with developer friends happy coding!

Never stop learning, here is your dose of my programming ashish donga blog hope you like this leave ur love in comment section

Top comments (0)