What is the difference between = and ===?

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

8 Likes

mood===‘sleepy’ => comparison

mood=‘sleepy’ => assignment

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

29 Likes

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

9 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.
46 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!

10 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

3 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

= 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'