Skip to content

Instantly share code, notes, and snippets.

@candraKriswinarto
Created June 22, 2022 02:17
Show Gist options
  • Save candraKriswinarto/38e52a600edf73f3b5626acc5c07403e to your computer and use it in GitHub Desktop.
Save candraKriswinarto/38e52a600edf73f3b5626acc5c07403e to your computer and use it in GitHub Desktop.

Useful JavaScript Code Snippets

  1. Sort an Array
//strings const names = ["Seema", "Rekha", "Jaya"]; names.sort(); //['Jaya', 'Rekha', 'Seema' ]

//Numbers const numbers = [101, 8, 87]; numbers.sort((a, b) => { return a - b; }); //[ 8, 87, 101 ]

  1. Select a random element
const items = ["Ball", "Bat", "Cup"] const randomIndex = Math.floor(Math.random()*items.length) items[randomIndex]
  1. Reverse a string
function reverseString(string) { return string.split(" ").reverse().join(" ") }

revereseString("Random String")

  1. Check if element has a class
const element = document.querySelector("#element") element.classList.contains("active")
  1. String interpolation
const name = "Jaya" console.log(`Hi, ${name}. You have ${2 ** 3} new notifications.`} //Hi, Jaya. You have 8 new notifications.
  1. Loop through an array
const cars = ["Ford", "BMW", "Audi" ] for (let car of cars) { console.log(car) }

/* Ford BMW Audi */

  1. Get current time
const date = new Date() const currentTime = `${date.getHours()}:${date.getMintues()}:${date.getSeconds()}`

console.log(currentTimes) //example output: "22:16:41"

  1. Replace part of a string
const string = "You are awesome." const replacedString = string.replace("You", "We")

console.log(replacedString) //Output: "We are awesome"

  1. Destructing variable assignment
let profile = ['bob', 34, 'carpenter']; let [name, age, job] = profile; console.log(name); // bob
  1. Using the spread operator
let data = [1,2,3,4,5]; console.log(...data); // 1 2 3 4 5 let data2 = [6,7,8,9,10]; let combined = [...data, ...data2]; console.log(...combined); // 1 2 3 4 5 6 7 8 9 10 console.log(Math.max(...combined)); // 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment