DeveloperBreeze

Detecting Browser and Version

// Immediately Invoked Function Expression (IIFE) to detect the browser and version
const browser = (function() {
    // Get user agent string from navigator
    const userAgent = navigator.userAgent;

    // Regular expression to match browser name and version
    const match = userAgent.match(/(chrome|firefox|safari|opera|edge|msie)\/(\d+(\.\d+)?)/i);

    // Return the detected browser information or 'Unknown' if not matched
    return match ? { name: match[1], version: match[2] } : 'Unknown';
})();

// Logging the detected browser information
console.log('Browser:', browser);

Related Posts

More content you might like

Tutorial
javascript

Running JavaScript in the Browser Console

     5 + 10 * 2;
  • Select an element on the webpage and access it in the console using $0:

Dec 10, 2024
Read More
Tutorial
javascript

Mastering console.log Advanced Usages and Techniques

You can measure the time it takes for a block of code to execute using console.time and console.timeEnd. This is useful for performance testing.

console.time('loop');
for (let i = 0; i < 1000000; i++) {
  // Some operation
}
console.timeEnd('loop'); // Output: loop: <time in ms>

Sep 02, 2024
Read More
Code
javascript

Checking if the User is Accessing from a Mobile Device in JavaScript

No preview available for this content.

Jan 26, 2024
Read More
Code
javascript

Validating Credit Card Numbers in JavaScript

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!