Reliable and Recognized JavaScript Certification Online #

A Boolean data type represents logical truth and falsehood and has only two values: literals true and false, which are also language reserved words.
Boolean values can be directly assigned to variables or are the result of a comparison.

var test1 = false;
var test2 = true;

var a = 5, b = 5;
var test3 = (a == 5); // true

Boolean values are often used in control structures. In if/else statement for example if a value is true one block of code is executed, if the value is false, then another:

var isFree = false;
var price;
if (isFree) {
    price = 0;
} else {
    price = 10;

All values in JavaScript can be converted to a boolean value.
Values that are converted to false:

  • “” (empty string)
  • 0
  • NaN
  • null
  • undefined

Values that are converted to true:

  • nonempty strings,
  • nonzero numbers,
  • objects (and arrays).

Flow control statements automatically perform this conversion:

var text = "";
if (!text) {
    alert("Text is empty");

var obj = {name: 'Jack'};
if (obj) {
    alert("Object is set");

In both cases alerts will be displayed.

No Comments