Select elements with querySelector and querySelectorAll
Select the first match with querySelector, which returns null on a miss, or all matches with querySelectorAll, a static NodeList. All modern browsers.
You need one element, or all of them, by CSS selector. The two methods differ in what they return and in what they do when nothing matches.
document.body.innerHTML = '<ul><li class="item">A</li><li class="item">B</li></ul>';
const first = document.querySelector('.item');
const all = document.querySelectorAll('.item');
const missing = document.querySelector('.nope');
document.querySelector('ul').append(document.createElement('li'));
console.log(first.textContent, all.length, missing);
console.log(document.querySelectorAll('li').length);A 2 null
3querySelector returns the first matching element in document order, or null, so read it with ?. when it may not exist. querySelectorAll returns a NodeList that is a snapshot: the li added afterwards is not in all, which still has length 2, and a new query sees three. Both are defined in the DOM Standard's ParentNode interface.
The snapshot differs from getElementsByClassName, whose result is live and updates as the page changes. A NodeList from querySelectorAll has forEach and iteration but not map, so spread it into an array first when you need array methods. An invalid selector throws a SyntaxError, so build selectors from user input with care.