JavaScript

20 Best JavaScript Snippets

Here are 20 useful JavaScript snippets that can help you when working on your projects:

1. Get current date and time:

const now = new Date();

2. Check if a variable is an array:

Array.isArray(variable);

3. Merge two arrays:

const newArray = array1.concat(array2);

4. Remove duplicates from an array:

const uniqueArray = [...new Set(array)];

5. Sort an array in ascending order:

array.sort((a, b) => a - b);

6. Reverse an array:

array.reverse();

7. Convert string to number:

const number = parseInt(string);

8. Generate a random number between two values:

const randomNumber = Math.floor(Math.random() * (max - min + 1)) + min;

9. Check if a string contains a substring:

string.includes(substring);

10. Get the length of an object:

Object.keys(object).length;

11. Convert object to array:

const array = Object.entries(object);

12. Check if an object is empty:

Object.keys(object).length === 0 && object.constructor === Object

13. Get current URL:

const currentUrl = window.location.href;

14. Redirect to a new URL:

window.location.replace(url);

15. Set a cookie:

document.cookie = "name=value; expires=date; path=path; domain=domain; secure";

16. Get a cookie:

const cookieValue = document.cookie.replace(/(?:(?:^|.*;\s*)name\s*\=\s*([^;]*).*$)|^.*$/, "$1");

17. Check if a cookie exists:

document.cookie.split(';').some((item) => item.trim().startsWith('name='))

18. Remove a cookie:

document.cookie = "name=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=path; domain=domain; secure";

19. Get the current viewport dimensions:

const viewportWidth = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0);
const viewportHeight = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0);

20. Copy text to clipboard:

navigator.clipboard.writeText(text);

These JavaScript snippets can help save time and simplify your code when working on web projects.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Eleftheria Drosopoulou

Eleftheria Drosopoulou is an Experienced Business Analyst with a demonstrated history of working in the computer software industry. Skilled in Computer Software Training, HTML Scripting and Microsoft Office. She is currently working as a director of Business Development in Exelixis Media P.C. and is the head of the Editorial team for "Java Code Geeks", "Web Code Geeks" and "System Code Geeks" websites.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button