- Use Array.prototype.includes() on the typeof values of both window and document (globals usually only available in a browser environment unless they were explicitly defined), which will return true if one of them is undefined.
- typeof allows globals to be checked for existence without throwing a ReferenceError.
- If both of them are not undefined, then the current environment is assumed to be a browser.
//#Source https://bit.ly/2neWfJ2 const isBrowser = () => ![typeof window, typeof document].includes('undefined'); console.log(isBrowser()); // true (browser) console.log(isBrowser()); // false (Node)
Improve this sample solution and post your code through Disqus
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join