ES6 – Boolean Primitive Datatype

ES6 – Boolean Primitive Datatype

It’s quite often useful to have values where we can differentiate between True/False or Yes/No. Surprisingly JavaScript and mostly other programing languages have Boolean datatype which has either has true or false as a value.

Mostly these boolean (true, false) values are generated by executing some expressions in JavaScript. However we have the ability to create them manually as well.


    

let isBigger = (3 > 2)
console.log(isBigger)
// true

 

let isSmaller = (3 < 2)
console.log(isSmaller)
// false

The mathematical symbols > and < are mostly used for comparison of data and values. They are also known as binary operators. Comparison statements using binary operators result boolean values. There is a list of ES6 – All Unary Operators you can checkout to learn more.

JavaScript Boolean Datatype:

The fact mostly boolean values are generated programmatically by executing JavaScript expressions, we are still able to assign boolean to variable manually.


    

let isTrue = true
console.log(isTrue)
// true

 

let isFlase = Boolean(false)
console.log(isFlase)
// false

JavaScript String Comparison:

We can apply binary operators on string datatype in same way. But the comparison would work in alphabetically order as shown in dictionary. Keep in mind below rules when comparing string in Javascript.

  • JavaScript compares the unicodes when string are used in comparison
  • Uppercase characters are smaller than lowercase characters
  • JavaScript perform as character by character comparison from left to right order

    

console.log(“Zoo” > “woods”)
// false

 

console.log(“Zoo” > “Woods”)
// true

JavaScript NaN Comparison:

There is only one value in JavaScript that is not equal to itself when comparing them. NaN is the only datatype and value in JavaScript that is in weird zone.


    

console.log(NaN == NaN)
// false

 

console.log(“NaN” == “NaN”)
// true
// as string not as NaN datatype

JavaScript Comparison:

Learn more about how JavaScript compares values and data using Unary Operators and Logical Operators  and how the boolean value is generated as the result.