what is difference between mood===‘sleepy’ and mood=‘sleepy’?
mood===‘sleepy’ => comparison
mood=‘sleepy’ => assignment
mood = 'sleepy'
is assigning the value ‘sleepy’ to the variable mood
.
mood === 'sleepy'
is testing whether the variable mood
has been assigned the value ‘sleepy’.
is there operator ‘==’ in JS? and if so what’s the difference to the ‘===’?
== looks at the value irrelevant of its type.
=== also checks if the type is the same.
Example:
2 == '2' // true, number and string have the same value so they are equal.
2 === '2' // false, number and string are not of the same type.
let money = 20000;
if(money === 20000){
console.log(‘I AM RICH’);
}
= IS USED TO ASSINGN A VALUE WHILE === IS USED IN CHECKING A VALUE…EXAMPLE;
THE = ASSIGNS THE VARIABLE MONEY A VALUE OF 20000…WHILE IN THE SECOND WE USED THE === TO CHECK IF THE VALUE OF MONEY IS 20000 SO;
= IS USED IN ASSIGNING VALUES!
=== IS USED IN CHECKING VALUES!
I’m just popping in to say this blew my mind. So simple once you get it. Thanks mtf!
Edit: Sorry i should have quoted what you said, still new here.
mood===‘sleepy’ => comparison
mood=‘sleepy’ => assignment
single equal operator= is use for assignment e.g mood=‘sleepy’ now mood variable will have a text(string) of sleepy. while === is use for comparison mood===‘sleepy’ it check whether mood and sleepy both have same data i.e ‘sleepy’ ===‘sleepy’ and its true…We also have another operator of comparison in Javascript and that is == the difference == and === is 10==‘10’ here datatype is left behind only fouces on the value/text and both are equal as both are 10. while if we say10===‘10’ it is false because here now focus is on both data and datatype 10===‘10’ the data is true but datatype is change left one is number but right one is string.
= assignment operator.
== is equal to but with out type.
=== is equal to with check type.
for ==
type or paste code here
let x = 10;
if ( x == '10' ) {
console.log( 'it works' );
} else {
console.log('it doesn't work');
}
//Output: It works because it doesn't check types is it string or others x = 10 and x == '10' similar no matters even if it is string in condition.
for ===
type or paste code here
let x = 10;
if ( x ==='10' ) {
console.log( 'it works' );
} else {
console.log('it doesn't work');
}
// Output: it doesn't work because type matters it should be x === 10 not '10'
‘=’ is used to assign things
while ‘===’ is used to find Boolean value(true or false)
Check this MDN link:
If I may add to this, albeit late. After reading through the comments, I went ahead and tried running all the ‘=’ variations (See code snippet below). I find that when used in the ‘if - else’ statement, they all yield the same result, all things being equal. Please try something like this and let me know if you get other results.
let mood = ‘sleepy’;
let tirednessLevel = 6;
if(mood === ‘sleepy’ && tirednessLevel > 8){
console.log(‘time to sleep’)
} else {
console.log(‘not bed time yet’)
}
EDIT: Just read the MDN Doc. I suppose for robustness, it is wise to use the strict equality comparison.
Simple and to the point! Thank you!
use triple === for Comparison Operators only and single = for Assignment Operators
Thanks. explained well !
Thank you,
This question was addressed earlier in the conversation, however your explaination was a little clearer to me.