14 Jun 2022 / by Alexander Schau
10 Javascript Tricks
10 Javascript Tricks that will save you a lot of time.
hero

Sometimes some simple tricks can save real time.

In the following I will share some of my favorite tips and tricks with you.

Feel free to share this article with your friends. Thanks.

1. Generate a random color

const randomColor = `#${Math.floor(Math.random() * 0xffffff).toString(16)}`;

console.log(randomColor);
window.open("https://example.com/", "_blank");

3. Replace all matches in a string

console.log("foobar".replace(/o/g, "-"));

4. Check a string for a specific substring

const string = "foobar";
const substring = "bar";

console.log(string.includes(substring));

5. Remove a value from an array

const array = ["a", "b", "c", "d"];

const index = array.indexOf("c");

if (index > -1) {
  array.splice(index, 1);
}

console.log(array);

6. Convert between strings and numbers

// number to string
console.log(100 + "");
// string to number
console.log(+"100");
Contact | Privacy Policy