SKU // 128

JS Navigator

The navigator object describes the browser and the device it is running on: which language the person uses, whether they are online, how many processor cores the device has. It is also the doorway to newer features like the clipboard and location. This lesson covers what it tells you, why you should be careful how you use it, and the right way to check what a browser can do.

// Facts About the Browser

Property Tells you
navigator.languagethe person's preferred language, like "en-US"
navigator.languagesan array of their preferred languages, most preferred first
navigator.onLinetrue if the device believes it has a network connection
navigator.cookieEnabledwhether cookies are allowed
navigator.hardwareConcurrencythe number of logical processor cores
navigator.maxTouchPointshow many fingers the screen can track (0 on a non-touch device)
navigator.userAgenta long text string naming the browser (see below)
console.log(typeof navigator.language, Array.isArray(navigator.languages));
console.log(navigator.languages[0] === navigator.language);
console.log(typeof navigator.onLine, typeof navigator.cookieEnabled);
console.log(navigator.hardwareConcurrency >= 1);
console.log(typeof navigator.maxTouchPoints);
string true true boolean boolean true number

Log navigator.language and navigator.languages to see yours. The first entry of languages is always the same as language.

// Language and Formatting

navigator.language is useful for showing numbers, dates, and money the way the person expects, with the toLocaleString you met in JS Data Types:

const price = 1234567.891;
console.log(price.toLocaleString(navigator.language));
// "1,234,567.891" for en-US, "1.234.567,891" for de-DE

// The User Agent String

navigator.userAgent is a long piece of text each browser sends to every website to say who it is. It looks like this (shortened):

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36

It is a historical mess. Almost every browser starts with Mozilla/5.0 and mentions several other browsers for compatibility, so the text often names a browser it is not. Do not write code like "if the string contains Chrome, do this". It breaks constantly and shuts out browsers you did not think of.

Detect features, not browsers
The question you really have is "can this browser do X?" Ask it directly (next section). Browser sniffing from userAgent gives wrong answers, and the string is slowly being frozen and trimmed by browsers for privacy.

// Feature Detection

Many newer abilities live on navigator. Before using one, check that it exists. The in operator asks whether a property is there:

console.log("geolocation" in navigator);
console.log("teleport" in navigator);

if ("clipboard" in navigator) {
  console.log("can use the clipboard");
} else {
  console.log("no clipboard here");
}
true false can use the clipboard

Code that checks first keeps working on older browsers instead of crashing with TypeError: Cannot read properties of undefined.

// Online and Offline

navigator.onLine is false when the device knows it has no network. The window fires "online" and "offline" events when that changes, which lets a page show a banner:

<p id="status"></p>

<script>
  function update() {
    document.getElementById("status").textContent =
      navigator.onLine ? "You are online" : "You are offline";
  }
  window.addEventListener("online", update);
  window.addEventListener("offline", update);
  update();
</script>
onLine can be fooled
true only means "connected to a network", not "can reach the internet". A device on wifi with no internet behind it still reports true. The reliable test is to try a request and handle the failure.

// The Clipboard

navigator.clipboard.writeText(text) copies text to the clipboard. It returns a promise, it works only on secure (https) pages, and it must be triggered by a user action such as a click. It can also be refused, so handle failure:

<button id="copy">Copy a message</button>
<p id="note"></p>

<script>
  document.getElementById("copy").addEventListener("click", async function () {
    try {
      await navigator.clipboard.writeText("Hello from my page");
      document.getElementById("note").textContent = "Copied";
    } catch (error) {
      document.getElementById("note").textContent = "Could not copy: " + error.name;
    }
  });
</script>

// Other Doors on navigator

Privacy
Combining small details (language, screen size, number of cores, installed fonts) can single out one device without any cookie. This is called fingerprinting. Browsers now limit what navigator reveals, which is another reason to ask for as little as you need.

// Quick Check

What is the right way to check whether a browser supports a feature?

  1. Look for the browser name in navigator.userAgent
  2. Check that the feature exists, for example "clipboard" in navigator
  3. Assume every browser supports it
  4. Try it and ignore any error
Show answer
B. Feature detection asks about the feature itself. The userAgent string is unreliable.

What does this log?

console.log("teleport" in navigator);
Show answer
false. The in operator is true only when the property exists, and there is no teleport on navigator.

Why is navigator.onLine === true not proof that you can reach a website?

Show answer
It only means the device is connected to a network. That network might have no internet access. The reliable test is to make a request and handle the failure.

// Try It

  1. Click Open Playground and run it. Compare your answers with a classmate's
  2. Add a line that logs how many languages navigator.languages holds
  3. Write an if that says "Touch screen" when navigator.maxTouchPoints is above 0
  4. Check whether "share" in navigator is true on your phone and on your computer
// Video Walkthroughs
Last updated: Oct 11, 2026, 7:02 PM ET