JavaScript Navigator

Detecting browser and system information

🧭 What is Navigator?

The Navigator object contains information about the browser and system. It helps you detect browser capabilities, user agent, platform, and more.


// Get browser information
console.log(navigator.userAgent);
console.log(navigator.platform);
                                    

Output:

Mozilla/5.0 (Windows NT 10.0; Win64; x64)...

Win32

Navigator Properties

🌐

userAgent

Browser identification string

navigator.userAgent
💻

platform

Operating system platform

navigator.platform
🗣️

language

Browser's preferred language

navigator.language
🔌

onLine

Check internet connection

navigator.onLine

🔹 Browser Detection

Detect which browser the user is using:

function detectBrowser() {
    let browser = "Unknown";
    
    if (navigator.userAgent.includes("Chrome")) {
        browser = "Google Chrome";
    } else if (navigator.userAgent.includes("Firefox")) {
        browser = "Mozilla Firefox";
    } else if (navigator.userAgent.includes("Safari")) {
        browser = "Safari";
    } else if (navigator.userAgent.includes("Edge")) {
        browser = "Microsoft Edge";
    }
    
    return browser;
}

console.log("Browser: " + detectBrowser());

Output:

Browser: Google Chrome

🔹 System Information

Get information about the user's system:

// System information
console.log("Platform: " + navigator.platform);
console.log("Language: " + navigator.language);
console.log("Languages: " + navigator.languages);
console.log("Online: " + navigator.onLine);
console.log("Cookies Enabled: " + navigator.cookieEnabled);

Output:

Platform: Win32

Language: en-US

Languages: en-US,en

Online: true

Cookies Enabled: true

🔹 Geolocation

Get user's location (requires permission):

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(
        function(position) {
            console.log("Latitude: " + position.coords.latitude);
            console.log("Longitude: " + position.coords.longitude);
        },
        function(error) {
            console.log("Error: " + error.message);
        }
    );
} else {
    console.log("Geolocation not supported");
}

Output:

Latitude: 40.7128

Longitude: -74.0060

🧠 Test Your Knowledge

Which property tells you if the user is online?