DEV Community

Cover image for 15 Short & Sweet JavaScript Snippets
Matin Imam
Matin Imam

Posted on Edited on

15 Short & Sweet JavaScript Snippets

JavaScript is an incredibly versatile and powerful programming language, widely used for web development. Whether you're a seasoned developer or just starting out, having a handy set of JavaScript snippets can save you time and streamline your coding process. In this article, I've compiled 15 short and sweet JavaScript snippets that cover a variety of tasks. Let's dive in!

01. Get current data and time

const currentDateTime = new Date();
console.log(currentDateTime);
⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน ⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน

02. Find max number in array

const number = [5, 2, 7, 10, 1]
const maxNumber = Math.max(...number)

// 10
⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน ⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน

03. Shuffle array

function shuffleArray(array) {
    return array.sort(() => Math.random() - 0.5);
}
⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน ⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน

04. Generate random number between 1 and 10

const randomNumber = Math.floor(Math.random() * 10) + 1;
console.log(randomNumber);

// 7
⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน ⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน

05. Convert string to lowercase

const str = 'Hello, World!'
console.log(str.toLowerCase());

// hello, world!
⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน ⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน

06. Check if even or odd number

const num = 5;
if (num % 2 === 0) {
    console.log('Number is even');
} else {
    console.log('Number is odd');
}

// 'Number is odd'
⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน ⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน

07. Create a simple 10 second countdown timer

let seconds = 5;
const countdown = setInterval(() => {
    console.log(seconds);
    seconds--;
    if (seconds < 0) {
        clearInterval(countdown);
        console.log('Countdown finished!');
    }
}, 1000);


// 5
// 4
// 3
// 2
// 1
// Countdown finsihed!
⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน ⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน

08. Convert array of numbers to array of strings

const numbers = [1, 2, 3, 4, 5];
const strings = numbers.map(String);
console.log(strings);

// ['1', '2', '3', '4', '5']
⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน ⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน

09. Remove duplicates

let arr = ["apple", "mango", "apple", "orange", "mango", "mango"];
const removeDuplicates = arr => [...new Set(arr)];

console.log(removeDuplicates(arr));

// ['apple', 'mango', 'orange']
⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน ⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน

10. Convert sentence to array of words

const sentence = "This is a sentence";
const words = sentence.split(" ");

console.log(words);

// ['This', 'is', 'a', 'sentence']
⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน ⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน

11. Repeat string

function repeatString(str, n){
    return str.repeat(n);
}

const repeatedStr = ('abc', 3);
console.log(repeatedStr);

// 'abcabcabc'
⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน ⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน

12. Find intersection of arrays

// Define the intersection function
const intersection = (a, b) => a.filter(value => b.includes(value));

// Example arrays
const arrayA = [1, 2, 3, 4, 5];
const arrayB = [4, 5, 6, 7, 8];

// Use the intersection function to find common elements
const result = intersection(arrayA, arrayB);

// Log the result to the console
console.log(result);

// [4, 5]
⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน ⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน

13. Create dynamic strings

const name = 'Matin Imam';
const greeting = `Hello, ${name}!`;
console.log(greeting); // "Hello, Matin Imam!"
⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน ⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน

14. Merge objects

const person = {name: 'Matin'};
const details = {work: 'Developer'};

const fullDetails = {...person, ...details};
console.log(fullDetails);

// {name: 'Matin', age: 30}
⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน ⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน

15. Redirect to new URL after a delay

setTimeout(() => location.href = "https://www.linkedin.com/in/matin-imam/", 5000);
⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน ⭐ Must Read: matin/ short sweet javascript snippets ee - ประเด็นร้อน

These 15 JavaScript snippets are just a glimpse of what you can achieve with a few lines of code. Whether you're manipulating arrays, strings, or working with dates and times, these snippets can help streamline your development process.


Connect with Me

If you enjoyed this post and want to connect, feel free to reach out to me on Medium. I'd love to connect and share more insights about software development!

Follow me on Medium

Top comments (0)