Browser Details using JavaScript

Browser Details using JavaScript

navigator in JavaScript can be used get the browser details.

Sample JavaScript code:

const fetchBrowserName = () => {

	let browserInfo = navigator.userAgent;
	let browserName;
	if ( 
		browserInfo.includes('Opera') || 
		browserInfo.includes('Opr') 
	) {
	
		browserName = 'Opera';
		
	} else if ( browserInfo.includes( 'Edg' ) ) {
	
		browserName = 'Edge';
		
	} else if (browserInfo.includes( 'Chrome' ) ) {
	
		browserName = 'Chrome';
		
	} else if ( browserInfo.includes( 'Safari' ) ) {
	
		browserName = 'Safari';
		
	} else if ( browserInfo.includes( 'Firefox' ) ) {
	
		browserName= 'Firefox';
		
	} else {
	
		browserName = 'unknown';
		
	}
	return browserName;
	
}

const strBrowserName = fetchBrowserName();
console.log(
	'Browser name is',
	strBrowserName
);
const browserLanguage = navigator.language;
console.log(
	'Browser Language is',
	browserLanguage
);
const browserPlatform = navigator.platform;
console.log(
	'Browser Platform is',
	browserPlatform
);

Output:

So, we can make use of navigator in JavaScript to capture the browser details.

Leave a Reply