Tag: data types

‘Boo who’ challenges us to write a function that checks if a given argument is classified as a boolean primitive. If so, return true, if it’s anything else, return false.

The solution, once written, is quite simple but there are a few key concepts we have to understand before we can get there.

Here’s what FCC gives us as a starting point:

JavaScript

1

2

3

4

5

6

functionbooWho(bool){

// What is the new fad diet for ghost developers? The Boolean.

returnbool;

}

booWho(null);

Now, we have to figure out… What the hell is a boolean primitive?

First, let’s consider what boolean means? Boolean is a word used in both math and computer science. In computer science, it generally refers to a data type or a variable that can have only one of two values, usually true or false.

Boolean variables are useful in controlling the logic of an algorithm and are generally used in conditional statements such:

JavaScript

1

2

3

4

5

if(bool){

//if bool is true, do something

}else{

//if bool is false, do something else

}

Second, what does primitive mean? Mozilla, as always, gives a pretty good description of a primitive. Basically, Mozilla tells us a primitive value ‘is data that is not an object and has no methods.’

Tangent on Primitive Types vs. Reference Type

According to the latest ECMAScript standard, JavaScript has 7 data types. 6 primitive types which are string, number, boolean, null, undefined and symbol and one reference type – object.

The difference between the two types becomes confusing when you consider that JavaScript provides constructor functions that allow you to wrap boolean, number and string values as objects, which changes them from primitive to reference type.

Consider the following:

JavaScript

1

2

3

4

5

6

7

varstr=newString('hello world');

//'hello world' is a primitive string value

//str is now a string object

typeofstr;//'object'

typeof'hello world';//'string'

Problems start to arise if you try to use a string object like a string primitive. Keep in mind that a string object is still an object, and behaves like one.

For instance:

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

varstr1='primitive string';

varstr2='primitive string';

str1===str2;//true

//however

varobj1=newString('object string');

varobj2=newString('object string');

obj1===obj2;//false

//string objects are two completely different objects and will only equal themselves

Given that JavaScript will allow you to use the methods associated with an object prototype on the primitive values (example below), it is best to use primitive types whenever possible.

JavaScript

1

2

3

//example of a string prototype object method used on a string primitive

'hello world'.toUpperCase();//'HELLO WORLD'

FYI, Effective JavaScript by David Herman has a good section on why primitive types should be preferred to using object wrappers and I derived some of my examples above from those found in his book.

Ok, enough about Primitive Types, hopefully this has been more than enough info to solve ‘Boo who’.

End of Tangent

Check out the Mozilla link above for further explanation of primitive values (or this one for info on the Boolean object), but for our purposes in solving ‘Boo who’ we now know that the only boolean primitives are true and false.

So, now we have to fill in the booWho function above, checking if bool equals true or false. In doing so, we’ll use the strict equality operator (===) vs. the loose equality operator (==), as the latter will convert the two values to a common type before checking equality, which is not what we want.

At this point, we’ve gone over all the tools you’ll need to write the booWho function, so if you’re working through the freeCodeCamp curriculum you may want to go try to solve it on your own, otherwise, read on…

Here’s the solution to ‘Boo who’ that I came up with:

JavaScript

1

2

3

4

5

6

functionbooWho(bool){

if(bool===true||bool===false){

returntrue;

}

returnfalse;

}

As you probably know, || is the logical operator ‘or’ in JavaScript, so this function will return true if bool equals true or false.

The freeCodeCamp wiki provides an even shorter solution using the ‘typeof’ operator, written by Rafase282.

JavaScript

1

2

3

functionbooWho(bool){

returntypeofbool==='boolean';

}

Both solutions solve the ‘Boo who’ algorithm challenge and pass all the test cases at freeCodeCamp.