Navigator Object
You'll learn how to use JavaScript's navigator object to gather information about the user's browser and device.
What Is the Navigator Object?
The navigator object is a built-in property of the window object that provides details about the browser and the device running your code. Think of it as your browser's ID card—it tells you what browser is being used, what operating system it's running on, and other environmental details.
Unlike document which represents the page content, navigator represents the application displaying that content.
Key Properties
userAgent
The userAgent property returns a string identifying the browser, its version, and the operating system:
console.log(navigator.userAgent);
// Example output:
// "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36..."
This is commonly used to detect what browser or device a user is running, though it can be unreliable since browsers can modify this string.
platform
The platform property identifies the operating system:
console.log(navigator.platform);
// Examples: "Win32", "MacIntel", "Linux x86_64"
Practical Example
function getBrowserInfo() {
const info = {
browser: navigator.userAgent,
os: navigator.platform
};
console.log("Browser:", info.browser);
console.log("Platform:", info.os);
}
getBrowserInfo();
Key Takeaway: The navigator object provides read-only information about the user's browser and device through properties like userAgent and platform, useful for analytics or conditional feature support.