Tari Ibaba

Tari Ibaba is a software developer with years of experience building websites and apps. He has written extensively on a wide range of programming topics and has created dozens of apps and open-source libraries.

How to Remove All Vowels From a String in JavaScript

To remove all vowels from a string in JavaScript, call the replace() method on the string with this regular expression: /[aeiou]/gi, i.e., str.replace(/[aeiou]/gi, ''). replace() will return a new string where all the vowels in the original string have been replaced with an empty string.

For example:

const str = 'coding beauty';

const noVowels = str.replace(/[aeiou]/gi, '');

console.log(noVowels); // cdng bty

The String replace() Method

The String replace() method takes two arguments:

  1. pattern – a pattern to search for in the given string. We used a regular expression for this, but it can also be a string.
  2. replacement – the string used to replace the matches of the specified pattern in the string. By passing an empty string (''), we remove all occurrences of this pattern in the given string.

Note: replace() does not modify the original string, but returns a new string. Strings are immutable in JavaScript.

Regular Expression Explained

We use the two forward slashes (/ /) to specify the start and end of the regular expression.

The [] characters are used to specify a pattern that matches any of a specific group of characters. For example, the pattern [abc] will match 'a', 'b', or 'c'. In the same way, the [aeiou] pattern will match any of the 5 vowel characters in the English alphabet.

The g (global) regex flag is used to match all occurrences of the regex pattern. Without this flag, only the first pattern match would be removed after calling replace().

const str = 'coding beauty';

// "g" regex flag not set
const noVowels = str.replace(/[aeiou]/i, '');

// Only first vowel removed
console.log(noVowels); // cding beauty

The i (ignore case) flag is used to perform a case-insensitive search for a regex match in the given string. This ensures that all vowels are removed from the string whether they are uppercased or not.

const str = 'cOding bEaUty';

// "i" regex flag NOT set
const noVowels1 = str.replace(/[aeiou]/g, '');

// Only lowercased vowels removed
console.log(noVowels1); // cOdng bEUty

// "i" regex flag set
const noVowels2 = str.replace(/[aeiou]/gi, '');

// All vowels removed
console.log(noVowels2); // cdng bty

For a comprehensive guide to regex patterns in JavaScript, check out this regular expression syntax cheat sheet from the MDN docs.

How to Get the Previous Page URL in JavaScript

We can get the previous page URL in JavaScript with the document.referrer property.

For example:

const previousPageUrl = document.referrer;

console.log(`Previously visited page URL: ${previousPageUrl}`);

document.referrer is a readonly property that returns the URL of the page used to navigate to the current page.

Here’s a more practical example:

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Coding Beauty Tutorial</title>
  </head>
  <body>
    <a href="get-last-page.html">Link</a>
  </body>
</html>

get-last-page.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Coding Beauty Tutorial</title>
  </head>
  <body>
    You visited this page from: <span id="previous-page"></span>
    <script src="get-last-page.js"></script>
  </body>
</html>

get-last-page.js

const previousPage = document.getElementById('previous-page');

previousPage.textContent = document.referrer;
Showing the previous page URL on the visited page.
Displaying the previous page URL on the visited page.

Limitations of document.referrer

The document.referrer property doesn’t always work though. It typically gives the correct value in cases where the user clicked a link on the previous page to navigate to the current page.

But if the user visited the URL directly by typing into the address bar or using a bookmark, document.referrer will have no value.

The previous page URL can't be displayed for a direct visit.
The previous page URL can’t be displayed for a direct visit.

document.referrer also won’t have a value if the clicked link was marked with the rel="noreferrer" attribute. Setting rel to noreferrer specifically prevents referral information from being passed to the webpage being linked to.

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Coding Beauty Tutorial</title>
  </head>
  <body>
    <a href="get-last-page.html">Link</a>
    <a href="get-last-page" rel="noreferrer">Link (noreferrer)</a>
  </body>
</html>
"rel=noreferrer" prevents access to referral information
“rel=noreferrer” prevents access to referral information.

Perhaps you would like to get the previous page URL so that you can navigate to that page. You can do this easily with the history.back() method.

get-last-page.js

...
const backButton = document.getElementById('back');

backButton.onclick = () => {
  history.back();
};

get-last-page.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Coding Beauty Tutorial</title>
  </head>
  <body>
    You visited this page from: <span id="last-page"></span> <br />
    <br />
    <button id="back">Back</button>
    <script src="get-last-page.js"></script>
  </body>
</html>
Navigating to the previous page.
Navigating to the previous page.

How to Get the URL of the Last Visited Page in JavaScript

We can get the last page URL in JavaScript with the document.referrer property.

For example:

const lastPageUrl = document.referrer;

console.log(`Last visited page URL: ${lastPageUrl}`);

document.referrer is a readonly property that returns the URL of the page used to navigate to the current page.

Here’s a more practical example:

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Coding Beauty Tutorial</title>
  </head>
  <body>
    <a href="get-last-page.html">Link</a>
  </body>
</html>

get-last-page.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Coding Beauty Tutorial</title>
  </head>
  <body>
    You visited this page from: <span id="last-page"></span>
    <script src="get-last-page.js"></script>
  </body>
</html>

get-last-page.js

const lastPage = document.getElementById('last-page');

lastPage.textContent = document.referrer;
Showing the last page URL on the visited page.
Displaying the last page URL on the visited page.

Limitations of document.referrer

The document.referrer property doesn’t always work though. It typically gives the correct value in cases where the user clicks a link on the last page to navigate to the current page.

But if the user visited the URL directly by typing into the address bar or using a bookmark, document.referrer will have no value.

The last page URL can't be displayed for a direct visit.
The last page URL can’t be displayed for a direct visit.

document.referrer also won’t have a value if the clicked link was marked with the rel="noreferrer" attribute. Setting rel to noreferrer specifically prevents referral information from being passed to the webpage being linked to.

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Coding Beauty Tutorial</title>
  </head>
  <body>
    <a href="get-last-page.html">Link</a>
    <a href="get-last-page" rel="noreferrer">Link (noreferrer)</a>
  </body>
</html>
"rel=noreferrer" prevents access to referral information
“rel=noreferrer” prevents access to referral information.

Perhaps you would like to get the last page URL so that you can navigate to that page. You can do this easily with the history.back() method.

get-last-page.js

...
const backButton = document.getElementById('back');

backButton.onclick = () => {
  history.back();
};

get-last-page.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Coding Beauty Tutorial</title>
  </head>
  <body>
    You visited this page from: <span id="last-page"></span> <br />
    <br />
    <button id="back">Back</button>
    <script src="get-last-page.js"></script>
  </body>
</html>
Navigating to the last visited page.
Navigating to the last visited page.

How to Use an Async Function in the React useEffect() Hook

In this article, we’ll look at different ways to easily call an async function inside the React useEffect() hook, along with pitfalls to avoid when working with async/await.

Call async Functions With then/catch in useEffect()

async functions perform an asynchronous operation in JavaScript. To wait for the Promise the async function returns to be settled (fulfilled or rejected) in the React useEffect() hook, we could use its then() and catch() methods:

In the following example, we call the fetchBooks() async method to fetch and display stored books in a sample reading app:

export default function App() {
  const [books, setBooks] = useState([]);

  useEffect(() => {
    // await async "fetchBooks()" function
    fetchBooks()
      .then((books) => {
        setBooks(books);
      })
      .catch(() => {
        console.log('Error occured when fetching books');
      });
  }, []);

  return (
    <div>
      {books.map((book) => (
        <div>
          <h2>{book.title}</h2>
        </div>
      ))}
    </div>
  );
}

async/await Problem: async Callbacks Can’t Be Passed to useEffect()

Perhaps you would prefer to use the async/await syntax in place of then/catch. You might try doing this by making the callback passed to useEffect() async.

This isn’t a good idea though, and if you’re using a linter it will inform you of this right away.

// ❌ Your linter: don't do this!
useEffect(async () => {
  try {
    const books = await fetchBooks();
    setBooks(books);
  } catch {
    console.log('Error occured when fetching books');
  }
}, []);

Your linter complains because the first argument of useEffect() is supposed to be a function that either returns nothing or returns a function to clean up side effects. But async functions always return a Promise (implicitly or explicitly), and Promise objects can’t be called as functions. This could cause real issues in your React app, such as memory leaks.

useEffect(async () => {
  const observer = () => {
    // do stuff
  };

  await fetchData();

  observable.subscribe(observer);

  // Memory leak!
  return () => {
    observable.unsubscribe(observer);
  };
}, []);

In this example, because the callback function is async, it doesn’t actually return the defined clean-up function, but rather a Promise object that is resolved with the clean-up function. Hence, this clean-up function is never called, and the observer is never unsubscribed from the observable, resulting in a memory leak.

So how can we fix this? How can we use the await operator with an async function in the useEffect() hook?

async/await Solution 1: Call async Function in IIFE

One straightforward wait to solve this problem is to await the async function in an immediately invoked function expression (IIFE):

const [books, setBooks] = useState([]);

useEffect(() => {
  (async () => {
    try {
      const books = await fetchBooks();
      setBooks(books);
    } catch (err) {
      console.log('Error occured when fetching books');
    }
  })();
}, []);

As the name suggests, an IIFE is a function that runs as soon as it is defined. They are used to avoid polluting the global namespace and in scenarios where trying an await call could cause problems in the scope containing the IIFE (e.g., in the useEffect() hook, or in the top-level scope for pre-ES13 JavaScript).

async/await Solution 2: Call async Function in Named Function

Alternatively, you can await the async function inside a named function:

useEffect(() => {

  // Named function "getBooks"
  async function getBooks() {
    try {
      const books = await fetchBooks();
      setBooks(books);
    } catch (err) {
      console.log('Error occured when fetching books');
    }
  }

  // Call named function
  getBooks();
}, []);

Remember the example using the observable pattern? Here’s how we can use a named async function to prevent the memory leak that occurred:

// ✅ Callback is not async
useEffect(() => {
  const observer = () => {
    // do stuff
  };

  // Named function "fetchDataAndSubscribe"
  async function fetchDataAndSubscribe() {
    await fetchData();
    observable.subscribe(observer);
  }

  fetchDataAndSubscribe();

  // ✅ No memory leak
  return () => {
    observable.unsubscribe(observer);
  };
}, []);

async/await Solution 3: Create Custom Hook

We can also create a custom hook that behaves similarly to useEffect() and can accept an async callback without causing any issues.

The custom hook could be defined this way:

export function useEffectAsync(effect, inputs) {
  useEffect(() => {
    return effect();
  }, inputs);
}

And we’ll be able to call it from multiple places in our code like this:

const [books, setBooks] = useState([]);

useEffectAsync(async () => {
  try {
    const books = await fetchBooks();
    setBooks(books);
  } catch (err) {
    console.log('Error occured when fetching books');
  }
});

With these three approaches, we can now easily use the await operator with async functions in the useEffect() hook.

Define Async Function Outside useEffect()

To define a named async function outside the useEffect() hook, you can wrap the function with the useCallback() hook:

const getBooks = useCallback(async () => {
  try {
    const books = await fetchBooks();
    setBooks(books);
  } catch (err) {
    console.log('Error occured when fetching books');
  }
}, []);

useEffect(() => {
  getBooks();
}, [getBooks]);

Without useCallback(), the getBooks() function will be re-created on every re-render, triggering useEffect() and causing performance problems.

How to Use Async / Await in the React useEffect() Hook

To await an async function in the React useEffect() hook, wrap the async function in an immediately invoked function expression (IIFE).

For example:

const [books, setBooks] = useState([]);

useEffect(() => {
  (async () => {
    try {
      // await async "fetchBooks()" function
      const books = await fetchBooks();
      setBooks(books);
    } catch (err) {
      console.log('Error occured when fetching books');
    }
  })();
}, []);

In this article, we’ll look at different ways to call an async function in the useEffect() hook, along with pitfalls to avoid when working with async/await.

Calling async Functions With then/catch in useEffect()

async functions perform an asynchronous operation in JavaScript. To wait for the Promise the async function returns to be settled (fulfilled or rejected) in the React useEffect() hook, we could use its then() and catch() methods:

In the following example, we call the fetchBooks() async method to fetch and display stored books in a sample reading app:

export default function App() {
  const [books, setBooks] = useState([]);

  useEffect(() => {
    // await async "fetchBooks()" function
    fetchBooks()
      .then((books) => {
        setBooks(books);
      })
      .catch(() => {
        console.log('Error occured when fetching books');
      });
  }, []);

  return (
    <div>
      {books.map((book) => (
        <div>
          <h2>{book.title}</h2>
        </div>
      ))}
    </div>
  );
}

async/await Problem: async Callbacks Can’t Be Passed to useEffect()

Perhaps you would prefer to use the async/await syntax in place of then/catch. You might try doing this by making the callback passed to useEffect() async.

This isn’t a good idea though, and if you’re using a linter it will inform you of this right away.


// ❌ Your linter: don't do this!
useEffect(async () => {
  try {
    const books = await fetchBooks();
    setBooks(books);
  } catch {
    console.log('Error occured when fetching books');
  }
}, []);

Your linter complains because the first argument of useEffect() is supposed to be a function that either returns nothing or returns a function to clean up side effects. But async functions always return a Promise (implicitly or explicitly), and Promise objects can’t be called as functions. This could cause real issues in your React app, such as memory leaks.


useEffect(async () => {
  const observer = () => {
    // do stuff
  };

  await fetchData();

  observable.subscribe(observer);

  // Memory leak!
  return () => {
    observable.unsubscribe(observer);
  };
}, []);

In this example, because the callback function is async, it doesn’t actually return the defined clean-up function, but rather a Promise object that is resolved with the clean-up function. Hence, this clean-up function is never called, and the observer never unsubscribed from the observable, resulting in a memory leak.

So how can we fix this? How can we use the await operator with an async function in the useEffect() hook?

async/await Solution 1: Call async Function in IIFE

One straightforward way to solve this problem is to await the async function in an immediately invoked function expression (IIFE):


const [books, setBooks] = useState([]);

useEffect(() => {
  (async () => {
    try {
      const books = await fetchBooks();
      setBooks(books);
    } catch (err) {
      console.log('Error occured when fetching books');
    }
  })();
}, []);

As the name suggests, an IIFE is a function that runs as soon as it is defined. They are used to avoid polluting the global namespace and in scenarios where trying an await call could cause problems in the scope containing the IIFE (e.g., in the useEffect() hook).

async/await Solution 2: Call async Function in Named Function

Alternatively, you can await the async function inside a named function:

useEffect(() => {

  // Named function "getBooks"
  async function getBooks() {
    try {
      const books = await fetchBooks();
      setBooks(books);
    } catch (err) {
      console.log('Error occured when fetching books');
    }
  }

  // Call named function
  getBooks();
}, []);

Remember the example using the observable pattern? Here’s how we can use a named async function to prevent the memory leak that occurred:

// ✅ Callback is not async
useEffect(() => {
  const observer = () => {
    // do stuff
  };

  // Named function "fetchDataAndSubscribe"
  async function fetchDataAndSubscribe() {
    await fetchData();
    observable.subscribe(observer);
  }

  fetchDataAndSubscribe();

  // ✅ No memory leak
  return () => {
    observable.unsubscribe(observer);
  };
}, []);

async/await Solution 3: Create Custom Hook

We can also create a custom hook that behaves similarly to useEffect() and can accept an async callback without causing any issues.

The custom hook could be defined this way:

export function useEffectAsync(effect, inputs) {
  useEffect(() => {
    return effect();
  }, inputs);
}

And we’ll be able to call it from multiple places in our code like this:

const [books, setBooks] = useState([]);

useEffectAsync(async () => {
  try {
    const books = await fetchBooks();
    setBooks(books);
  } catch (err) {
    console.log('Error occured when fetching books');
  }
});

With these three approaches, we can now easily use the await operator with async functions in the useEffect() hook.

How to Quickly Swap Two Variables in JavaScript

1. Temporary Variable

To swap two variables in JavaScript:

  1. Create a temporary variable to store the value of the first variable
  2. Set the first element to the value of the second variable.
  3. Set the second variable to the value in the temporary variable.

For example:

let a = 1;
let b = 4;

// Swap variables
let temp = a;
a = b;
b = temp;

console.log(a); // 4
console.log(b); // 1

2. Array Destructuring Assignment

In ES6+ JavaScript we can swap two variables with this method:

  1. Create a new array, containing both variables in a particular order.
  2. Use the JavaScript array destructing syntax to unpack the values from the array into a new array that contains both variables in a reversed order.

With this method, we can create a concise one-liner to do the job.

let a = 1;
let b = 4;

[a, b] = [b, a];

console.log(a); // 4
console.log(b); // 1

How to Rotate an Image with JavaScript

This article was republished here: https://plainenglish.io/blog/javascript-rotate-image-ad2f05eafeb2

To rotate an image with JavaScript, access the image element with a method like getElementById(), then set the style.transform property to a string in the format rotate({value}deg), where {value} is the clockwise angle of rotation in degrees.

Consider this sample HTML:

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript Rotate Image</title>
  </head>
  <body>
    <div style="margin: 8px">
      <img src="my-image.jpg" width="300" />

      <!-- The image element to rotate -->
      <img id="rotated" src="my-image.jpg" width="300" />
    </div>

    <script src="index.js"></script>
  </body>
</html>

Here’s how we can easily rotate the #rotated image element from JavaScript:

index.js

// Access DOM element object
const rotated = document.getElementById('rotated');

// Rotate element by 90 degrees clockwise
rotated.style.transform = 'rotate(90deg)';

And this will be the result on the web page:

The image on the right is rotated 90 degrees clockwise.

First, we access the DOM element object with the document.getElementById() method.

Then, we use the style.transform property of the object to set the transform CSS property of the element from JavaScript.

We can specify any angle between 1 and 359 for the rotation:

// Access DOM element object
const rotated = document.getElementById('rotated');

// Rotate element by 180 degrees clockwise
rotated.style.transform = 'rotate(180deg)';
Rotating an image by 180 degrees clockwise using JavaScript.
The image on the right is rotated 180 degrees clockwise.

Rotate Image Counter-Clockwise

We can rotate an image in the counter-clockwise direction by specifying a negative angle.

// Access DOM element object
const rotated = document.getElementById('rotated');

// Rotate image by 90 degrees counter-clockwise
rotated.style.transform = 'rotate(-90deg)';
Rotating an image by 90 degress counter-clockwise using JavaScript.
The image on the right is rotated by 90 degrees counter-clockwise.

We can specify any angle between -1 and -359 degrees for counter-clockwise rotation.

// Access DOM element object
const rotated = document.getElementById('rotated');

// Rotate image by 135 degrees counter-clockwise
rotated.style.transform = 'rotate(-135deg)';
Rotating an image by 135 degrees counter-clockwise using JavaScript.
The image on the right is rotated 135 degrees counter-clockwise.

Customize Image Transform Origin

We can use the transform-origin CSS property to set the point that the image will be rotated about. transform-origin is center by default, which means the element will be rotated about its center point.

In the following example, we rotate the image 90 degrees clockwise, as we did in the first example in this article.

// Access DOM element object
const rotated = document.getElementById('rotated');

// Rotate image by 90 degrees clockwise
rotated.style.transform = 'rotate(90deg)';

// Rotate image about top-left corner
rotated.style.transformOrigin = 'top left';

But this time we customize the transform-origin property, so the image element ends up at a different position after the rotation.

The image on the right is rotated about its top-left point
The image on the right is rotated about its top-left point.

Rotate Image on Button Click

To rotate the image at the click of a button, assign an event handler function to the onclick attribute of the button element.

For example:

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript Rotate Image</title>
  </head>
  <body>
    <div>
      <!-- Assign "rotateImage()" to "onclick" attribute -->
      <button id="rotate" onclick="rotateImage()">Rotate image</button>

      <!-- The image element to be rotated -->
      <img id="rotated" src="my-image.jpg" width="300" />
    </div>

    <script src="index.js"></script>
  </body>
</html>

Here the rotateImage() function serves as the click event handler. It contains the logic for rotating the image, and will be called when the button is clicked.

index.js

// Access DOM element object
const rotated = document.getElementById('rotated');

function rotateImage() {
  // Rotate image by 90 degrees clockwise
  rotated.style.transform = 'rotate(90deg)';
}

Now when the button is clicked, the image will be rotated 90 degrees in the clockwise direction.

The image is rotated 90 degress clockwise on button click.

Rotate Image Incrementally

We can incrementally rotate the image on button click by storing the angle of rotation in a variable and using this variable to get and update the current angle. Many image editing apps allow you to rotate images in 90-degree angle increments.

index.js

// Access DOM element object
const rotated = document.getElementById('rotated');

// Variable to hold the current angle of rotation
let rotation = 0;

// How much to rotate the image at a time
const angle = 90;

function rotateImage() {
  // Ensure angle range of 0 to 359 with "%" operator,
  // e.g., 450 -> 90, 360 -> 0, 540 -> 180, etc.
  rotation = (rotation + angle) % 360;

  rotated.style.transform = `rotate(${rotation}deg)`;
}
The image is rotated incrementally on button click.

4 Quick Fixes for the “Cannot Read Property ‘length’ of Undefined” Error in JavaScript

Are you experiencing the “cannot read property ‘length’ of undefined” error in JavaScript? This error occurs when you attempt to access the length property from a variable that has a value of undefined.

const arr = undefined;

// TypeError: Cannot read properties of undefined (reading 'length')
const length = arr.length;

console.log(length);

To fix the “cannot read property ‘length’ of undefined” error, perform an undefined check on the variable before accessing the length property from it. There are various ways to do this.

1. Use an if Statement

We can use an if statement to check if the variable is truthy before accessing the length property:

const arr = undefined;

let arrLength = undefined;

// Check if "arr" is truthy
if (arr) {
  arrLength = arr.length;
}

console.log(arrLength); // undefined


const str = undefined;

let strLength = undefined;

// Check if "str" is truthy
if (str) {
  strLength = str.length;
}

console.log(strLength); // undefined

2. Use Optional Chaining

We can use the optional chaining operator (?.) to return undefined and prevent the method call if the variable is nullish (null or undefined):

const arr = undefined;

// Use optional chaining on "arr" variable
const arrLength = arr?.length;

console.log(arrLength); // undefined


const str = undefined;

// Use optional chaining on "str" variable
const strLength = str?.length;

console.log(strLength); // undefined

3. Access the length Property of a Fallback Value

We can use the nullish coalescing operator (??) to provide a fallback value to access the length property from.

const arr = undefined;

// Providing an empty array fallback
const arrLength = (arr ?? []).length;

console.log(arrLength); // 0


const str = undefined;

// Providing an empty string fallback
const strLength = (str ?? '').length;

console.log(strLength); // 0

The nullish coalescing operator (??) returns the value to its left if it is not null or undefined. If it is, then ?? returns the value to its right.

console.log(2 ?? 5); // 2
console.log(undefined ?? 5); // 5

We can also use the logical OR (||) operator to provide a fallback value from which to access the length property.

const arr = undefined;

// Providing an empty array fallback
const arrLength = (arr || []).length;

console.log(arrLength); // 0

const str = undefined;

// Providing an empty string fallback
const strLength = (str || '').length;

console.log(strLength); // 0

Like the ?? operator, || returns the value to its left if it is not null or undefined. If it is, then || returns the value to its right.

4. Use a Fallback Value Instead of Accessing the length Property

We can also combine the optional chaining operator (?.) and the nullish coalescing operator (??) to provide a fallback value to use as the result, instead of accessing the length property from the undefined value.

const arr = undefined;

// Using "0" as a fallback value
const arrLength = arr?.length ?? 0;

console.log(arrLength); // 0


const str = undefined;

// Using "0" as a fallback value
const strLength = str?.length ?? 0;

console.log(strLength); // 0

As we saw earlier, the logical OR operator (||) can replace the ?? operator in cases like this:

const arr = undefined;

// Using "0" as a fallback value
const arrLength = arr?.length || 0;

console.log(arrLength); // 0

const str = undefined;

// Using "0" as a fallback value
const strLength = str?.length || 0;

console.log(strLength); // 0

How to Easily Convert Any HTML String to Markdown in JavaScript

We can use the Turndown library to easily convert HTML to markdown in JavaScript.

To get started with Turndown, we can install it from NPM using this command:

Shell
npm i turndown

After the installation, we’ll be able to import it into a JavaScript module like this:

JavaScript
import TurndownService from 'turndown'

For a Common JS module, we’ll import it like this instead:

JavaScript
const TurndownService = require('turndown');

Now we can use the turndown module to easily convert any HTML string to markdown:

JavaScript
import TurndownService from 'turndown'; const html = ` <h1>Learn Web Development</h1> <p>Check out <a href="https://codingbeautydev.com/blog">Coding Beauty</a> for some great tutorials!</p> `; // Create an instance of the Turndown service const turndownService = new TurndownService(); const markdown = turndownService.turndown(html); console.log(markdown);

This code will have the following output:

Markdown
Learn Web Development ===================== Check out [Coding Beauty](https://codingbeautydev.com/blog) for some great tutorials!

Use Turndown in browser with script tag

We can also use Turndown in a browser by importing the Turndown script using a script tag:

HTML
<script src="https://unpkg.com/turndown/dist/turndown.js"></script>

After including the script, we’ll be able to convert HTML to Markdown just as easily as we did in the previous code example:

JavaScript
const html = ` <h1>Learn Web Development</h1> <p>Check out <a href="https://codingbeautydev.com/blog">Coding Beauty</a> for some great tutorials!</p> `; // Create an instance of the Turndown service const turndownService = new TurndownService(); const markdown = turndownService.turndown(html); console.log(markdown);

In the browser, we can also pass DOM nodes as input to Turndown:

JavaScript
// convert document <body> to Markdown const bodyMarkdown = turndownService.turndown(document.body); // convert first <div> tag to Markdown const divMarkdown = turndownService.turndown(document.body);

Customize HTML to Markdown conversion

We can pass options to Turndown to customize how it should convert an HTML string to Markdown. Options can be specified in the constructor when creating a new instance of the Turndown service.

JavaScript
import TurndownService from 'turndown'; const html = ` <ul> <li>HTML</li> <li>CSS</li> <li>JavaScript<li>`; // Specifying options when creating an instance of the // Turndown service const turndownService = new TurndownService({ bulletListMarker: '-' }); const markdown = turndownService.turndown(html); console.log(markdown);

Here, we use the bulletListMarker property to specify that Turndown should use the - symbol to indicate a list item in the Markdown. So this will be the output of the code:

Markdown
- HTML - CSS - JavaScript

The bulletListMarker also accepts other values, like the * character:

JavaScript
import TurndownService from 'turndown'; const html = ` <ul> <li>HTML</li> <li>CSS</li> <li>JavaScript<li>`; // Specifying options when creating an instance of the // Turndown service const turndownService = new TurndownService({ bulletListMarker: '*' }); const markdown = turndownService.turndown(html); console.log(markdown);

This will produce the following output:

Markdown
* HTML * CSS * JavaScript

How to Add Seconds to a Date in JavaScript

1. Date setSeconds() and getSeconds() Methods

To add seconds to a date in JavaScript, use the getSeconds() method to get the seconds, then call the setSeconds() method on the date, passing as an argument the sum of getSeconds() and the number of seconds to add.

For example:

function addSeconds(date, seconds) {
  date.setSeconds(date.getSeconds() + seconds);
  return date;
}

// 12:00:00 AM on April 17, 2022
const date = new Date('2022-04-17T00:00:00.000Z');

// 12:00:20 AM on April 17, 2022
const newDate = addSeconds(date, 20);

console.log(newDate); // 2022-04-17T00:00:20.000Z

Our addSeconds() function takes a Date object and the seconds to add as arguments. It returns a new Date object with the newly added seconds.

The Date getSeconds() method returns a number between 0 and 59 that represents the seconds of a particular date.

The Date setSeconds() sets the seconds of a date to a specified number.

If the seconds added would increase the minutes, hour, day, or year of the Date, setSeconds() automatically updates the Date information to reflect this.

// 12:00:00 AM on April 17, 2022
const date = new Date('2022-04-17T00:00:00.000Z');

// Add 100 seconds to date
date.setSeconds(date.getSeconds() + 100);

// 12:01:40 AM on April 17, 2022
console.log(date); // 2022-04-17T00:01:40.000Z

In this example, passing 100 to setSeconds() increments the minutes of the date by 1 (60 seconds) and sets the seconds to 40.

Avoiding Side Effects

The setSeconds() method mutates the Date object it is called on. This introduces a side effect into our addSeconds() function. To avoid modifying the passed date and create a pure function, make a copy of the date and call setSeconds() on this copy, instead of the original:

function addSeconds(date, seconds) {
  // Making a copy with the Date() constructor
  const dateCopy = new Date(date);
  dateCopy.setSeconds(date.getSeconds() + seconds);

  return dateCopy;
}

const date = new Date('2022-04-17T00:00:00.000Z');

const newDate = addSeconds(date, 20);
console.log(newDate); // 2022-04-17T00:00:20.000Z

// Original not modified
console.log(date); // 2022-04-17T00:00:00.000Z

Tip

Functions that don’t modify external state (i.e., pure functions) tend to be more predictable and easier to reason about. This makes it a good practice to limit the number of side effects in your programs.

2. date-fns addSeconds()

Alternatively, you can use the pure addSeconds() function from the date-fns NPM package to quickly add seconds to a date in JavaScript.

import { addSeconds } from 'date-fns';

const date = new Date('2022-04-17T00:00:00.000Z');

const newDate = addSeconds(date, 20);
console.log(newDate); // 2022-04-17T00:00:20.000Z

// Original not modified
console.log(date); // 2022-04-17T00:00:00.000Z