Skip to main content
Version: 2.0.3

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.

Informationโ€‹

  • Unit tests: โœ…

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