JavaScript Struggles – Part 4 | Comparing

Here we go again!

This one is very easy but we all have to know it, you may already heard about it somewhere else too.
If you weren’t familiar with JS’s comparing way keep going. If you’re then this post is not for you. 😊👏🏻

There’s two ways of c…


This content originally appeared on DEV Community and was authored by

Here we go again!

Welcome back

This one is very easy but we all have to know it, you may already heard about it somewhere else too.
If you weren't familiar with JS's comparing way keep going. If you're then this post is not for you. 😊👏🏻

There's two ways of comparing variables in JavaScript:

Two Equal Signs (==)

Most majority of programming languages uses == as the one and only comparing operator, but in JavaScript we're special. 😏

== is only comparing the value of the variable, ignoring the data type of it; so if there's a number that is equal to a number inside the string it'll always be true.

E.g.

console.log('0' == 0); // Outputs: true

console.log(1 == true); // Outputs: true

The data type of the variable will not be changed after the comparison

Some of Its Uses
We can use it to check if the number is not 0 or an empty string.

let num = 0;
let word = "";

console.log(num == true); // Outputs: false
console.log(word == true); // Outputs: false

num = 5;
word = "Hey!";

console.log(num == true); // Outputs: true
console.log(word == true); // Outputs: true

Three Equal Signs (===)

We use this as the normal comparing operator, that's only working with JavaScript, TypeScript, PHP.

What === actually does is that it compares the value and the data type.

E.g.

console.log("0" === 0); // Outputs: false

/* They must be of the same data type. */
console.log("0" === "0"); // Outputs: true

=== is the most used one. Probably because it's the easy way of conparing and we all understand it even if JavaScript wasn't your first language.

Thanks for reading! 😌

I hope this may be helpful for someone.




This content originally appeared on DEV Community and was authored by


Print Share Comment Cite Upload Translate Updates
APA

| Sciencx (2021-11-28T07:39:32+00:00) JavaScript Struggles – Part 4 | Comparing. Retrieved from https://www.scien.cx/2021/11/28/javascript-struggles-part-4-comparing/

MLA
" » JavaScript Struggles – Part 4 | Comparing." | Sciencx - Sunday November 28, 2021, https://www.scien.cx/2021/11/28/javascript-struggles-part-4-comparing/
HARVARD
| Sciencx Sunday November 28, 2021 » JavaScript Struggles – Part 4 | Comparing., viewed ,<https://www.scien.cx/2021/11/28/javascript-struggles-part-4-comparing/>
VANCOUVER
| Sciencx - » JavaScript Struggles – Part 4 | Comparing. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/28/javascript-struggles-part-4-comparing/
CHICAGO
" » JavaScript Struggles – Part 4 | Comparing." | Sciencx - Accessed . https://www.scien.cx/2021/11/28/javascript-struggles-part-4-comparing/
IEEE
" » JavaScript Struggles – Part 4 | Comparing." | Sciencx [Online]. Available: https://www.scien.cx/2021/11/28/javascript-struggles-part-4-comparing/. [Accessed: ]
rf:citation
» JavaScript Struggles – Part 4 | Comparing | | Sciencx | https://www.scien.cx/2021/11/28/javascript-struggles-part-4-comparing/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.