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 Fix the “Cannot read Property ‘push’ of Undefined Error in JavaScript

The “cannot read property ‘push’ of undefined” error in JavaScript occurs when you try to call the push() method on a variable intended to contain an array, but actually contains a value of undefined.

An example of the cannot read property 'push' of undefined error occurring in JavaScript.

This could be caused by calling the push() method on:

  1. a variable without first initializing it with an array.
  2. an array element instead of the array itself.
  3. an object property that does not exist or has a value of undefined.

We’ll explore practical solutions for all these possible causes in this article.

1. Calling push() on an uninitialized variable

To fix the “cannot read property ‘push’ of undefined” error, ensure that the variable has been initialized with an array before calling the push() method on it.

let doubles;

let nums = [1, 2, 3, 4, 5];

for (const num of nums) {
  let double = num * 2;

  // ❌ TypeError: cannot read properties of undefined (reading 'push')
  doubles.push(double);
}

console.log(doubles);

In the example above, we called the push() method on the doubles variable without first initializing it.

let doubles;

console.log(doubles); // undefined

Because an uninitialized variable has a default value of undefined in JavaScript, calling push() causes an error to be thrown.

To fix the error, all we have to do is to assign the doubles variable to an array (empty for our use case):

// ✅ "doubles" initialized before use
let doubles = [];

let nums = [1, 2, 3, 4, 5];

for (const num of nums) {
  let double = num * 2;

  // ✅ push() called - no error thrown
  doubles.push(double);
}

console.log(doubles); // [ 2, 4, 6, 8, 10 ]

2. Calling push() on an Array object

To fix the “cannot read property ‘push’ of undefined” error, ensure that you didn’t access an element from the array variable before calling push(), but instead called push() on the actual array variable.

const array = [];

// ❌ TypeError: Cannot read properties of undefined (reading 'push')
array[0].push('html');
array[0].push('css');
array[0].push('javascript');

console.log(array);

Accessing the 0 property with bracket indexing gives us the element at index 0 of the array. The array has no element, so arr[0] evaluates to undefined and calling push() on it causes the error.

To fix this, we need to call the push on the array variable, not one of its elements.

const array = [];

// ✅ Call push() on "array" variable, not "array[0]"
array.push('html');
array.push('css');
array.push('javascript');

console.log(array); // [ 'html', 'css', 'javascript' ]

3. Calling push() on an object’s property that is undefined

To fix the “cannot read property ‘push’ of undefined” error in JavaScript, ensure that the object property that you are calling the push() method on exists and is not undefined.

const students = [
  { name: 'Mac', scores: [80, 85] },
  { name: 'Robert' },
  { name: 'Michael', scores: [90, 70] },
];

// ❌ TypeError: Cannot read properties of undefined (reading 'push')
students[1].scores.push(50);

In this case, the error came about because the object element at the index 1 doesn’t have a scores property.

const obj = {};

console.log(obj.prop); // undefined

Accessing a non-existent property from an object doesn’t throw an error in JavaScript, but rather gives you a value of undefined. It’s if you try to call a method like push() on that non-existent property that you will encounter an error.

In this case, we can fix the error by setting the score property of the second array element to a defined value.

const students = [
  { name: 'Mac', scores: [80, 85] },
  // ✅ Fixed: "scores" set to a defined value
  { name: 'Robert', scores: [] },
  { name: 'Michael', scores: [90, 70] },
];

// ✅ "scores" property exists, "push()" works - no error thrown
students[1].scores.push(50);

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://wp.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://wp.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://wp.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