What is the difference between = and ===?

what is difference between mood===‘sleepy’ and mood=‘sleepy’?

5 Likes

mood===‘sleepy’ => comparison

mood=‘sleepy’ => assignment

52 Likes

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’.

21 Likes

is there operator ‘==’ in JS? and if so what’s the difference to the ‘===’?

7 Likes

== 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.
31 Likes

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!

8 Likes

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

2 Likes

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.

1 Like