For web developers, Boolean variables have only two possible values: true or false. Computers need a way to determine whether something is true or false in order to make decisions. The decision-making process makes it possible for a computer to perform a task, to choose between two tasks, or to stop performing a task.

Boolean variables are traditionally used to make a decision or to tell the user the truth value of the Boolean variable (possibly as the result of performing a calculation or checking the status of data).

In most cases, you create a Boolean variable by assigning the variable a value of true or false like this: var MyBoolean = true. You can also assign a variable a Boolean value by using an expression that equates to true or false, such as var MyBoolean = 3 < 4. In this case, 3 is less than 4, so MyBoolean is true. The < symbol is an operator.

You can create Boolean values by using the new operator. The statement MyBoolean = new Boolean(); creates a new Boolean variable that’s initialized to false. You can also add a value or an expression between the parentheses. Some odd things happen in this situation.

For example, if you provide MyBoolean = new Boolean("Hello");, JavaScript creates a Boolean variable with a value of true. The variable is true because the string you supplied isn’t empty — it contains a value. This is one of several techniques you can use to test other variables for content in JavaScript.

The Boolean new operator accepts all sorts of inputs. The following list of inputs creates variables that contain a value of false:

  • 0

  • -0

  • null

  • ""

  • false

  • undefined

  • NaN

The NaN keyword stands for Not a Number. It occurs when you perform certain esoteric math functions. In addition, some JavaScript functions return this value when you use them incorrectly. For example, if you call parseInt(“Hello”), the parseInt() function returns NaN because “Hello” isn’t a number and parseInt() can’t turn it into a number.