Have you ever written an exam where you wrote the right answer yet failed? Eg. A scenario where you’re asked 1 + 4 = ? and you answer with “Five” and fail the question? Well, a lot of students of the University of Lagos went through this recently when they wrote some online tests on their University’s platform, this issue went viral on Twitter Nigeria not long ago. While the average human can tell when the answer to a question is correct regardless of how it is written, machines aren’t able to except you teach them how to. If you’re curious as to how that can happen keep reading. In the field of software engineering, the sign “=” means a totally different thing. While the average person reads “=” as “equal to” that same convention doesn’t apply when you’re a software engineer nor does it apply to code, we’ll examine this in Javascript today.
“=” is an assignment operator in Javascript (and most programming languages if not all). What this means is that if I say var number = 5; I have simply told the computer that wherever you see "number" I am referring to 5. What I did there was that I assigned the value of 5 to a variable which I named number.
“==” is a comparison operator in Javascript. It is used to compare two values in a bid to perform an operation based on the results of the comparison of those numbers. Eg.
if (10 == 10) {
var outcome = "10 is equal to 10";
}
Console.log(outcome);
Using Javascript code, I told the computer “If 10 is the same thing as 10 log into the console of the words 10 is equal to 10”. The machine will compare the 10 before the == operator and the 10 after the == operator to see if they’re the same thing before performing the instruction I gave to it.
“===” is also a comparison operator, only it doesn’t just compare values, it compares data types. Eg.
if (10 == “10”) {
var outcome = "10 is equal to 10";
}
Console.log(outcome);
10 == “10” are not the same, the first 10 is of an integer data type and the second “10” is of a string data type which is why it is found inside “”. The “” sign is used to denote string data type values.