Skip to content

About

A simple JavaScript function that checks whether a given value is a boolean primitive (true or false).

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Build a Boolean Check Function — freeCodeCamp

Solution for the freeCodeCamp lab “Build a Boolean Check Function.”

Challenge

Write a function booWho that:

  1. Receives one argument.
  2. Returns true if the argument is a boolean primitive (true or false).
  3. Returns false for any other value (numbers, strings, arrays, objects, NaN, etc.).

Solution

See booWho.js.

function booWho(bool) {
  return typeof bool === "boolean";
}

How it works: typeof returns the string "boolean" only for the two boolean primitives, true and false. For every other type, typeof returns something else ("number", "string", "object", etc.), so the function returns false.

Test Cases

Input Output
booWho(true) true
booWho(false) true
booWho([1, 2, 3]) false
booWho([].slice) false
booWho({ "a": 1 }) false
booWho(1) false
booWho(NaN) false
booWho("a") false
booWho("true") false
booWho("false") false

About

A simple JavaScript function that checks whether a given value is a boolean primitive (true or false).

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages