Free, independent guides since 2010

Code

Modern JavaScript basics for website builders

The JavaScript you need for a modern website: let and const, arrow functions, querySelector, events, fetch with async/await, modules and when not to use JavaScript at all.

<guide section="Code" type="guide" read="2 min"/>

JavaScript has changed enormously since we wrote our introduction to JavaScript and its history. The language gets a new edition every year, and browsers now support features that used to require libraries like jQuery. This guide covers the modern basics a website builder actually needs.

Variables: const and let

JavaScript
const siteName = 'dom4j.org';   // cannot be reassigned
let visits = 0;                  // can change
visits = visits + 1;

Use const by default and let only when the value really changes. The old var has confusing scoping rules and is best left in old code.

Functions and template literals

JavaScript
const greet = (name) => `Welcome back, ${name}!`;
console.log(greet('Sander'));  // Welcome back, Sander!

Finding elements and reacting to clicks

JavaScript
const button = document.querySelector('#menu-toggle');
const menu = document.querySelector('.menu');

button.addEventListener('click', () => {
  const open = menu.classList.toggle('is-open');
  button.setAttribute('aria-expanded', open);
});

querySelector accepts any CSS selector, querySelectorAll returns all matches. Updating aria-expanded tells screen readers whether the menu is open – a small step towards accessibility.

Loading data with fetch and async/await

JavaScript
async function loadArticles() {
  try {
    const response = await fetch('/api/articles.json');
    if (!response.ok) throw new Error(`HTTP ${response.status}`);
    const articles = await response.json();
    for (const a of articles) {
      console.log(a.title);
    }
  } catch (error) {
    console.error('Could not load articles:', error);
  }
}
loadArticles();

Modules: organise your code

HTML
<script type="module" src="app.js"></script>
JavaScript
// utils.js
export function formatPrice(n) {
  return new Intl.NumberFormat('en-IE', { style: 'currency', currency: 'EUR' }).format(n);
}
// app.js
import { formatPrice } from './utils.js';
console.log(formatPrice(19.95)); // €19.95

Module scripts are deferred automatically. For classic scripts, add defer so they do not block rendering.

When not to use JavaScript

The fastest script is the one you never load. Many things that once needed JavaScript are now pure HTML or CSS: <details> for show/hide blocks, <dialog> for pop-ups, CSS for animations and smooth scrolling, loading="lazy" for images. Less JavaScript means a faster site and better Core Web Vitals, especially the responsiveness measure INP.

The MDN JavaScript Guide is an excellent, free next step.

Frequently asked questions

Do I still need jQuery?

For new projects, rarely. Modern browsers support everything jQuery was originally used for. Existing sites can keep it, but there is no need to add it just for selecting elements or doing requests.

Does JavaScript hurt SEO?

Not by itself, but content that only appears after scripts run can be indexed later or less reliably. Put your important content and links in the HTML, and use JavaScript to enhance the page.