[freeCodeCamp] Basic JavaScript - Queues, Boolean, Comparison Operators
Tags: javascript, webdev, react, angular
Published: 2020-06-24 18:41:46

Howdy fellows! Here is yet another JavaScript post covering the fundamentals. This is in continuation of my JavaScript learning from [freeCodeCamp](https://www.freecodecamp.org/). The last post in the series can be found [here](https://dev.to/gutsytechster/freecodecamp-basic-javascript-functions-edj), where we have learned about functions in JavaScript.

Let's get started then.

# <u>Queues</u>

Queues are the abstract data structure, in which items are kept in a specific order. The items are added from the back to the queue and taken out from the front of the queue.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/n09spl1gv2y6xqqafb36.png)
*<center>Queue data structure</center>*


The above image describes the queue very well. You can see two terms in it - `enqueue` and `dequeue`. Let's know what these are

- When an element is pushed to the queue, the operation is known as `enqueue`.
- When an element is popped out of the queue, the operation is known as `dequeue`.

A queue can also be seen as an array, with some restrictions.

The following example will help you understand it better

```
var  arr = [1, 2, 3]
```

Adding a number to the end of an array can be done with `push()` method. Similarly popping out from the front of an array can be done using `shift()` method.

```
arr.push(4);  // enqueue, now array [1, 2, 3, 4]
arr.shift();  // dequeue, now array [2, 3, 4]
```

# <u>Boolean</u>

`Boolean` is one of the basic data types in JavaScript. `Boolean` can only have two values which are either `true` or `false`.

Remember that `true` and `false` are keywords in JavaScript. If you'll put them into the quotes, they'll be treated as normal strings.

# <u>Comparison Operators</u>

There are various comparison operators in JavaScript, let's have a look at them

- <h3><u>Equality Operator</u></h3>

JavaScript has an equality operator `==`. It compares two values and returns either `true` or `false`.

> To compare two different data types(for e.g. `numbers` and `string`), it must convert one type to another. This is known as *Type Coercion*.

Some examples of equality operator are

```
1   ==  1   // true
1   ==  2   // false
1   == '1'  // true
"3" ==  3   // true
```

As you may notice, the last two examples return `true` irrespective of different data types i.e. this operator does not check for equality of data types.

- <h3><u>Strict Equality Operator</u></h3>

Strict Equality operator is represented by `===`. This not only checks for the value but also checks for the data type of its two operands. Let's see an example

```
4 === 4;  // true
"4" === 4; // false
```

The second comparison returns `false` because `"4"` is a string while `4` is a `number`.

> In JavaScript, we can get the type of a value using the `typeof` operator as

```
typeof "3"; // returns 'string'
typeof 3;  // returns 'number'
```

- <h3><u>Inequality operator</u></h3>

It is the opposite of the equality operator. It returns `true` when two values given to it are not equal, `false` otherwise. Just like the equality operator, it doesn't check for the data type of its operands.

Some examples of it can be seen as

```
1 !=  2     // true
1 != "1"    // false 
1 != true   // false
0 != false  // false
```

- <h3><u>Strict Inequality Operator</u></h3>

This would be clear by now. Strict inequality operator is represented by `!==`. This not only checks for the value but also for the data type. It is just the opposite of the strict equality operator. Let's understand its usage with some examples:

```
3 !==  3   // false
3 !== '3'  // true
4 !==  3   // true
```

As you may notice, that the second case returns `true` because the data type for the values is different.

> It is highly advisable to use strict equality and strict inequality operator instead of their non-strict options available.

- <h3><u>Greater than operator (`>`)</u></h3>

It compares two values and returns `true` if the value to the left of it is greater than the value to the right. One thing to note down here is that it converts the data type of the values before comparing, just like the equality operator.

A few examples can be

```
7   > '3'  // true
2   >  3   // false
'1' >  9   // false
```

- <h3><u>Greater than or equals to operator (`>=`)</u></h3>

It checks if the value to the left of it is either greater or equal when compared to the value right of it.

```
7   >= '3'  // true
2   >=  3   // false
'7' >=  9   // false
```

- <h3><u>Less than operator (`<`)</u></h3>

It is quite obvious now. It checks if the value to the left of it is lesser than the value to the right of it. Returns `true` if so, `false` otherwise.

```
'3' < 7  // true
3 < 2    // false 
'8' < 4  // false
```

- <h3><u>Less than or equals to operator (`<=`)</u></h3>

Let's directly go with an example for this

```
5   <= 5  // true
3   <= 2  // false
'8' <= 4  // false
```

# <u>Conclusion</u>

With the end of this short post, we have acquired some knowledge about the Boolean data type and Queue data structure. Apart from it, we found out about type coercion and how they work when used with comparison operators. 

# <u>References</u>

- [Introduction to JavaScript](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/)
- [Conquering freeCodeCamp - Basic JavaScript (Part 2) - Live Stream #12](https://www.youtube.com/watch?v=lkOXlVgKHYM&list=PLgBH1CvjOA62oNEVgz-dECiCZCE_Q3ZFH&index=13&t=0s)

Let's meet next time with another JavaScript post covering other fundamentals of JavaScript. Till then be curious and keep learning.