is.browser
is.browser(target: unknown)​
The is.browser
method is used to determine if the current environment is a browser or not. It returns true
if the current environment is a browser and false
otherwise.
This method is useful when you have code that runs differently in the browser than in other environments, such as Node.js. For example, you may need to use the window object or interact with the DOM in the browser, but not in other environments.
Note that this method relies on the presence of the window
object to determine if the environment is a browser, so it may not be reliable in all cases.
Specification​
- Target argument: optional.
- Return: boolean.
Examples​
is.browser() // true if the command is executed in the browser
is.browser(window.navigator) // Returns true if the navigator is not empty
// Alternative
IsConfig.state.navigator = window.navigator;
is.browser() // Returns true if the navigator is not empty
is.browser(false) // false
is.browser(new Boolean(0)) // false
is.browser(new Boolean()) // false
is.browser(0) // false
is.browser('') // false
is.browser({}) // false
is.browser([]) // false
// And all other known types will return false
Reversal boolean result​
is.not.browser() // false if the command is executed in the browser
is.not.browser(window.navigator) // Returns false if the navigator is not empty
// Alternative
IsConfig.state.navigator = window.navigator;
is.not.browser() // Returns false if the navigator is not empty
is.not.browser(false) // true
is.not.browser(new Boolean(0)) // true
is.not.browser(new Boolean()) // true
is.not.browser(0) // true
is.not.browser('') // true
is.not.browser({}) // true
is.not.browser([]) // true
// And all other known types will return true