1. javaScript

Boolean in JavaScript

A Boolean in Logic Maths represents truth or falsehood of a value. Boolean could either be true or false only.

Similarly in JavaScript a boolean variable could only hold two values – true or false. Boolean is very helpful for structuring JavaScript code properly and control flow of code while program runs.

  • Comparing values
  • Controlling flow of code across the program
  • Comparing values

Comparing multiple variables or a variable with some constant is needed frequently in Software Programs. For example – Let’s take example of Google Maps, for it to work there’s need of doing many calculations and then comparing numbers(distances in maps) as well. Let’s see some examples how comparing of values could be done in JavaScript –

a == 4 (Would return => true if a is 4 or false otherwise)

  • Controlling flow of code across the program

Coding is just making certain decisions here and there to get a final destination or to get some app to work properly. There’s a lot of decision making involved in coding, in context of JavaScript this decision making can be done using (if else) statements alongwith Booleans. Let’s understand how this could be done using an example –

if(a == 4)
   b = b + 1;
   a = a + 1;

Here if a is 4 then b would be incremented by 1 or otherwise a itself will be increased by 1. Just making simple decisions.

But as JavaScript code gets larger and larger, this aspect of Control of Flow becomes quite important. Even in my day to day life, I use this quiet frequently not only in JavaScript coding rather in other languages as well. Because fundamental principle is same, just syntax varies.

That’s it, I hope that you find this useful. In case if your learning JavaScript Programming Language, I’ve made written a whole lot of content about it on this website. You can easily check it out here – JavaScript Computer Science Hub.

If you have any additional question, do comment here and I would reply as soon as possible for me. Thanks

Comments to: Boolean in JavaScript

Your email address will not be published.