Howdy fellows! Here is yet another JavaScript post covering the fundamentals. This is in continuation of my JavaScript learning from freeCodeCamp. The last post in the series can be found here, where we have learned about functions in JavaScript.
Let's get started then.
Queues
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.
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]
Boolean
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.
Comparison Operators
There are various comparison operators in JavaScript, let's have a look at them
Equality Operator
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
andstring
), 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.
Strict Equality Operator
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'
Inequality operator
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
Strict Inequality Operator
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.
Greater than operator (
>
)
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
Greater than or equals to operator (
>=
)
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
Less than operator (
<
)
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
Less than or equals to operator (
<=
)
Let's directly go with an example for this
5 <= 5 // true
3 <= 2 // false
'8' <= 4 // false
Conclusion
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.
References
Let's meet next time with another JavaScript post covering other fundamentals of JavaScript. Till then be curious and keep learning.
Top comments (0)